Link to home
Start Free TrialLog in
Avatar of Siddharth S
Siddharth S

asked on

Datepicker in PHP

Hello All,
I have a php form to fill in some details. Below is the code
<html>

<body>



<h1>Form to update new tags into DB</h1>



<form action="insert4.php" method="POST" onsubmit="return confirm('Are you sure you want to submit this form for updating a new tag?');">

<table>
Service Tag:     &nbsp&nbsp&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<input type="text" name="stagn" /><br><br>

Purchase Order Id: &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<input type="text" name="poidn" /><br><br>

Purchase Order Date (dd format): &nbsp&nbsp &nbsp &nbsp &nbsp<input type="text" name="podnd" /><br><br>

Purchase Order Month (mm format) : &nbsp  </pre> <input type="text" name="podnm" /><br><br>

Purchase Order Year (yyyy format): &nbsp &nbsp <input type="text" name="podny" /><br><br>


 
 
</body>
</html>


<input type="submit" />

<div class="form-container">

</form>

</body>
</html>


<html>

<body>

Open in new window


Upon clicking on submit the code performs an action called insert4.php
Below is the code for insert4.php. This basically updates MySQL database.
<html>
<body>
    <?php
    $con = mysql_connect("localhost","xxx","xxxx");

    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }


    if($_POST['podnd'] > 31 || $_POST['podny'] > date("Y") || $_POST['podnm'] > 12 || $_POST['podny']<date("Y")-1)
    {
        die('Error: Check the Dates entered' . mysql_error());
    }

    if(strlen($_POST['stagn']) != 7)
    {
        die('Error: Check the tag entered. Service Tag is a 7 charecter alphanumeric code'.mysql_error());
    }

    mysql_select_db("software_it", $con);

    $sql="INSERT INTO purchaseorders (ServiceTag, PurchaseOrderDate,PurchaseOrderYear,PurchaseOrderMonth,PurchaseOrderId) VALUES
        ('$_POST[stagn]','$_POST[podnd]','$_POST[podny]','$_POST[podnm]','$_POST[poidn]')";

    if (!mysql_query($sql,$con))
{
    if (mysql_errno() == 1062 || mysql_errno() == 1586 || mysql_errno() == 1859 || mysql_errno() == 3026) 
    {
         die('Error: New Tags only. Please use other link for updating existing records' );
    }
}
	else
	{
		echo "1 record added. Wait for 5 seconds.";
	}
	
	/*header( "refresh:5;url=test4.php" )*/;
	
    mysql_close($con)
	
	
	
    ?>
</body>
</html>

Open in new window


Could you please let me know how to add the datepicker in my form (the first code) and what variable should I input in my second code. I tried using JavaScript but have been unsuccessful.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of James Allan
James Allan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Siddharth S
Siddharth S

ASKER

Hi James,

I would like to add the datepicker as another field (text or date) in the form and assign it to a variable which can be inturn used to update in the DB by using the insert4.php action.

Users accessing this form should be able to choose the date from the datepicker and once clicked on submit, it needs to update in the DB.

Thanks,
SS.
Do you know jquery basics ?
Nvm James,

I figured it out. I was actually having problem in parsing the date from php to MySQL. I had MySQL column datetype defined as date, when I changed this to text, it started working. Thanks for your time. I am good now..

Since you tried to help, I will mark your answer as the solution.  :)

Thanks James,
Cheers.
Thanks
Going forward, you might want to bookmark these articles.  They teach the underlying principles and correct usage of PHP date/time processing.

Procedural
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html

Object-Oriented
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
Ok!
Btw i had made an example for you.
Click on your dates inputs to display datepicker

In case you want to test look this.

<!DOCTYPE html>
<html>
<head>
	<title>testing</title>
</head>
<body>
<form action="" method="post">

<table>
Service Tag:     &nbsp&nbsp&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<input type="text" name="stagn" /><br><br>

Purchase Order Id: &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<input type="text" name="poidn" /><br><br>

Purchase Order Date (dd format): &nbsp&nbsp &nbsp &nbsp &nbsp<input class="from_date" type="text" name="podnd" /><br><br>

Purchase Order Month (mm format) : &nbsp  </pre> <input  class="from_date"  type="text" name="podnm" /><br><br>

Purchase Order Year (yyyy format): &nbsp &nbsp <input class="from_date"  type="text" name="podny" /><br><br>
</table>
</form>




</body>
<script
  src="https://code.jquery.com/jquery-3.1.1.js"
  integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
  crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
<script >

$(document).ready(function(){  
         $.datepicker.setDefaults({
         	dateFormat:"dd-mm-yy"
         })   
$(function(){  

        $(".from_date").datepicker();  
       
	});  
	
        
});  

</script>
</html>

Open in new window

You may want to try this PHP date picker package that uses a jQuery calendar plugin to let thge user pick dates and even submit them to the server using AJAX.
Comments really helped. Thank you folks. I am good now. :)