0

i have a form in html that contains an input type=date, and a php code to post the informations in a database,

php:

if(isset($_POST['Ajouter'])){ // Fetching variables of the form which 
travels in URL
$date = $_POST['date'];



$requete="insert into absences(IdAbs, DateAbs, IdEmp ) values ('1', '$date', 
  '3')";
$query = mysqli_query($db,$requete);

html :

<!Doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body> 
<form action="testabs.php" method="post">
<input id="date" type="date" name="date"> 
<input type="Submit" value="Ajouter" name="Ajouter">*
</form>
</body>
</html>

i don't know how to post the type=date, can anyone help me with this?

mery
  • 43
  • 7
  • Just like how you would post a text input. – frz3993 May 18 '18 at 17:25
  • Depends what format the date is in on the form and the type of SQL field you are entering it into. If you are using the SQL date format, then the date will need to go in as xxxx-xx-xx, if its just a text field then as @fr3993 says, its just another piece of text. – Stephen May 18 '18 at 17:28
  • it gives me an error "Undefined index : datein C:\wamp\www\gestion absence\testabs.php on line 26" – mery May 18 '18 at 17:28
  • Probably means the date isn't being posted properly and the value of $_POST['date'] isn't set when the PHP script is executed – Stephen May 18 '18 at 17:29
  • @Stephen i'm using the SQL date format, so i have to change the input's format? – mery May 18 '18 at 17:30
  • This has nothing to see with your problem but please read https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1, your code isn't safe – gogaz May 18 '18 at 17:30
  • If the browser supports datepicker, when you picked and post a date, the browser will always send the date in Y-m-d format, despite what you might see in the browser. Tested in chrome, edge and firefox – frz3993 May 18 '18 at 17:42
  • 1
    Until you get proper testing or use the developer tools in your browser, you can insert this code `die(print_r($_POST));` to see if what PHP is getting is what you expect. – Jeff Harris May 18 '18 at 18:11

1 Answers1

1

If you are getting an undefined index it probably means the date isn't being posted. Try:

if(isset($_POST['date']))
{
    $date= $_POST['date'];
}
else
{
    $date= "2020-02-02";
}

Or if using PHP7+

$date= (isset($_POST['date']) ? $_POST['date'] : "2020-02-02");

If you end up with 2020-02-02 in your database, you have a post issue.

To change the format to suit SQL date field:

$inputDate= new dateTime($_POST['date']);
$date= $inputDate->format('Y-m-d');

You still need to add a suitable check to see if the date actually exists in the first place.

Stephen
  • 395
  • 1
  • 15