gpt4 book ai didi

javascript - 使用 JS 检查输入的 mysql DB

转载 作者:行者123 更新时间:2023-12-03 07:21:31 25 4
gpt4 key购买 nike

&我有一个用于收货的数量字段。我想根据订单表检查收据表,并确保输入的值不超过订单总值(value)。我在我的领域有这个代码:

<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);
}

我是一个完全的新手,以防有人想知道。我非常感谢您的帮助!

最佳答案

起初我以为您是在问如何在数据库中执行此操作,但现在我看到您已经从服务器获取数据并将其返回。所以我现在就回答,希望是你的意思。

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
}
}
});
}

我会这么说......每次字段更改时,您基本上都会触发服务器请求和数据库请求。这是极其低效的。您应该只加载一次数据,然后在本地引用它进行检查。但我会让你自己解决这个问题:)

关于javascript - 使用 JS 检查输入的 mysql DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36196072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com