This is a short tutorial to show to capture a datepicker date from a HTML web form and capture it in a MYSQL table. The steps are as follows:
1. Create a web page ADD_FORM.php file with a form object and assign a datepicker object. This will be used to capture the data and send it to a second php file which writes it to a MySql database. This involves setting up links to the appropriate js libraries in the header section. Next, create the web form and and datepicker object in the body of the web page.
2. Create a php file to capture the data from the \web_\form page.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Create a Web Page ADD_FORM.php and Form Object and Assign a Datepicker Object
- Here is my code for ADD_FORM.php. You will note that the initial header section is native html and in fact you could write all the code in HTML.
- Header section - see attached file.
- The relevant form element is shown in the attached file. The form calls the next php file - insert_record.php
Step 2: Insert Your Datepicker Field Into the Mysql Database
I am not going to cover the complete php file but the relevant parts are as follows:
1. $due_date= $_POST['datepicker'];
Please ensure that you escape the variable to prevent sql injections.
$due_date = mysql_real_escape_string($due_date);
Also ensure that references to your database environment are not in webroot, e.g. if /var/www/html is web root, place it outside this.
Also use PDO (PHP Data Objects) to insert your date using a parameterized SQL query. - ensure that your data field has the right data type
due_date | datetime | YES | | NULL
An example of the captured data in MySql is as follows:
This is the default MYSql date format. That's it, good luck