我想将datepicker中的日期插入我的msql数据库,但是在ajax查询中调用错误函数而不是成功,并且未插入日期。
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);
}
日期选择器正常工作并显示在文本字段中选择的日期,但我无法插入所选日期。我相信这可能是关于PHP的错误。非常感谢帮助。