&我有一个货物接收的数量字段。我想根据订单表检查收货表,并确保输入的值不超过总订单价值。我在我的领域有这个代码:
<div class="pure-control-group">
<label for="rec_qty">Qty Received:</label>
<input type="text" name="rec_qty" id="rec_qty" onChange="receiptTotal()"></input>
</div>
Javascript代码:
function receiptTotal(){
$.ajax({
type: "GET",
url: "receipts.php?do=checkrec&id="+row.pur_ID,
//dataType: 'json',
success : function(data) {
// Here I want to do the comparisons of the two fields.
// If data.rec_qty + form.rec_qty > pur_qty then throw error.
}
});
}
最后PHP代码:
if($_GET['do'] == "checkrec") {
$rec = [];
//First get the receipts total for this ID
$getRows = $db->query(sprintf("SELECT sum(rec_qty) as total_qty, pr.pur_qty from receipts inner join purchases pr on rec_purID = pr.pur_ID WHERE rec_purID = '$groupid' GROUP BY pur_ID")) or SQLError();
while($rec = $getRows->fetch_assoc()) {
$result[] = $rec;
}
echo json_encode($result);
}
我是一个完整的新手只是因为有人在想。我真的很感激帮助!
答案 0 :(得分:1)
起初我以为你在询问如何在数据库中执行此操作,但现在我看到你已经从服务器获取数据并将其返回。所以我现在回答,希望是你的意思。
function receiptTotal(){
$.ajax({
type: "GET",
url: "receipts.php?do=checkrec&id="+row.pur_ID,
//dataType: 'json',
success : function(data) {
var rec_qty = $('#rec_qty').val();
if((data.rec_qty+rec_qty) > data.pur_qty){
alert('Value is too high!!'); // or whatever error you want
}
}
});
}
我会说这个......虽然每次字段更改时你基本上都会发出服务器请求和数据库请求。这是非常低效的。您应该只加载一次数据,然后在本地引用它进行检查。但我会让你自己解决这个问题:)