我正在制作送餐应用程序。截至目前,我的应用程序无法提交订单。它一直说“订单未添加”尽管页面上有输入且订单没有通过。我相信验证有问题。当我检查控制台时,没有错误。
更新:我已根据要求添加了PHP。
JavaScript的:
$("#btnAddOrder").bind("click", function () {
AddOrder();
});
function AddOrder() {
if ($("#orderform").valid()) {
var url = serverURL() + "/placeorder.php";
var JSONObject = {
"itemID": localStorage.getItem("itemID"),
"price": localStorage.getItem("price"),
"quantity": $('#qtytooorder').val(),
"requestedDateTime": $('#deliverby').val(),
"userid": localStorage.getItem("userid"),
};
$.ajax({
url: url,
type: 'GET',
data: JSONObject,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (arr) {
_getAddOrderResult(arr);
},
error: function () {
validationMsg();
}
});
}
else {
validationMsgs("Please select a date and quantity", "Error", "OK");
}
}
function _getAddOrderResult(arr) {
if (arr[0].result === 1) {
$("#deliverby").val("");
$("#qtytoorder").val("");
validationMsgs("Order added.", "Info", "OK");
window.location = "Orders.html"; //bring him to me.html
}
else {
validationMsgs("Order is not added.", "Error", "OK");
}
}
HTML:
<form name="orderform" id="orderform">
Please deliver by:
<input type="datetime-local" id="deliverby">
Quantity to order:
<input type="text" id="qtytoorder" value="1" />
<input type="button" value="Order" id="btnAddOrder">
</form>
PHP:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
include("global.php");
try{
$conn = new mysqli(server, dbuser, dbpw, db);
$userID = $_GET["userID"];
$itemID = $_GET['itemID'];
$price = $_GET['price'];
$quantity = $_GET['quantity'];
$requestedDateTime = $_GET['requestedDateTime'];
$query = "insert into orders (userID, itemID, price, quantity, requestedDateTime, status) values ('$userID','$itemID',$price,$quantity,'$requestedDateTime','ORD')";
$result = $conn->query($query);
if (!$result){
$json_out = "[" . json_encode(array("result"=>0)) . "]";
}
else {
$json_out = "[" . json_encode(array("result"=>1)) . "]";
}
echo $json_out;
$conn->close();
}
catch(Exception $e) {
$json_out = "[".json_encode(array("result"=>0))."]";
echo $json_out;
}
?>