I would like to insert the date from datepicker into my msql database but the error function is called in ajax query instead of success and the date is not inserted.
HTML:
<form id="dForm">
<input type="date" name="date" id="date">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
JAVASCRIPT:
$(function(){
$("#my-page").submit(function(e){
e.preventDefault();
var formData = $("#dForm :input")
.datepicker("getDate").serialize();
console.log(formData);
$.ajax({
url: "php/file.php",
type: "POST",
data: formData,
dataType: "json",
async: "false",
encode: true,
success: function(response)
{
if(response.status == 'success')
{
console.log(response);
$.mobile.changePage("#next-page");
}
},
error: function(response)
{
alert("error.");
console.log(response);
}
});
});
});
PHP:
include_once("db.php");
if(!empty($_POST["date"])){
$date = strtotime($_POST["date"]);
$date = date("Y-m-d",$date);
$q = "INSERT INTO table (date) VALUES ('$date')";
$res = ($q);
$array = array();
if (mysqli_query($conn, $res)) {
$array["status"] = "success";
$array["message"] = "successful";
$array["date"] = $form_date;
}else{
$array["status"] = "error";
$array["message"] = "failed";
header('HTTP/1.1 401 Unauthorized', true);
}
echo json_encode($array);
}
The date picker works correctly and shows the date selected in the textfield but I cannot insert the date selected. I believe it may be an error regarding the php. Help would very much be appreciated.