时间将保存到数据库中为00:00:00



当前,我正在尝试将表单保存到数据库中。然而,当我尝试将时间保存到数据库中时,它被保存为00:00:00。我使用的数据类型是时间。可能是因为表单使用hh:mm AM/PM格式,因此表单详细信息没有保存到数据库中吗?

HTML:

<div data-role="fieldcontainer">
<label for="time_from">From</label>
<input type="time" name="time_from">
</div>
<div data-role="fieldcontainer">
<label for="time_to">To</label>
<input type="time" name="time_to">
</div>

JS:

function AddBooking() {
var url = serverURL() + "/submitform.php";
var JSONObject = {
"time_from": $('#time_from').val(),
"time_to": $('#time_to').val()
}
$.ajax({
url: url,
type: 'GET',
data: JSONObject,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (arr) {
_getApplicationResult(arr);
},
error: function () {
validationMsg();
}
});
}
function _getAddorderResult(arr) {
if (arr[0].result === 1) {
validationMsgs("Application submitted.", "Info", "OK");
window.location = "homepage.html";
}
else {
validationMsgs("Application is not submitted", "Error", "OK");
}
}

我认为问题出在这里:

var JSONObject = {
"time_from": $('#time_from').val(),
"time_to": $('#time_to').val()
}

您正试图访问id为"的元素;time_from";以及";time_to";但您的DOM似乎没有。。。尝试将id属性添加到输入元素中,例如:

<input type="time" name="time_to" id="time_to">
"time_from": $('#time_from').val(),
"time_to": $('#time_to').val()

Hash(#(用于通过id属性访问对象

你需要这样的东西:

"time_from": $("input[name='time_from']").val(),
"time_to": $("input[name='time_to']").val(),

最新更新