我正在使用一个由动态值组成的表单,例如在单击按钮时添加表行,我正在使用 jQuery 执行此操作,但现在我什至想在将数据插入我的数据库之前对其进行验证。
我必须使用外部 PHP 文件进行此验证我正在使用 AJAX 进行此验证,但验证运行良好,但数据仍被插入到我的数据库中。我已经尽力了,但它仍然不适合我。
这是我的代码:
function update_db(){
var udata = {};
var adata = {};
del_query = new Array();
var confirm = 0;
var a = "1";
if ($("#maintable tbody tr").length>0){
var vendorid = $("#vendorinfo").val();
// prepare data to be updated
$('[id^="dbtr_"]').each(function(index, table){
var rid = $('th:eq(0)', this).find('input:eq(0)').val();
var basecatid = $('td:eq(0)', this).find('select:eq(0)').val();
var subvendorid = $('td:eq(0)', this).find('select:eq(0)').val();
var prodid = $('td:eq(1)', this).find('input:eq(1)').val();
var productname = $('td:eq(1)', this).find('input:eq(0)').val();
var quantity = $('td:eq(2)', this).find('input:eq(0)').val();
var muc = $('td:eq(3)', this).find('#muid').val();
var amt = $('td:eq(4)', this).find('input:eq(0)').val();
var reason = $('td:eq(5)', this).find('textarea:eq(0)').val();
var vat_percentage = $('td:eq(5)', this).find('input:eq(0)').val();
var total_amount_before_vat = $('td:eq(5)', this).find('input:eq(1)').val();
var vat_charged_in_bill = $('td:eq(5)', this).find('input:eq(2)').val();
var invoice = $('td:eq(6)', this).find('input:eq(0)').val();
if(invoice =='' && prodid !=''){
alert("Invoice Number Cannot Be Empty");
$("#savetodb").prop("disabled", true);
a = "0";
return false;
}
if( quantity !='' && invoice !=''){
var vouchdt = $("#dateinfo").val();
$.ajax({
type: "POST",
url: "../model/check_procurement_resold_with_invoice_number.php",
data: { para : "upd", invno : invoice, product : prodid, date : vouchdt, quantity : quantity},
success: function(result){
if(result == "") {
alert(productname+" does not exist for invoice number "+invoice);
a = "0";
return false
}
if(result == "2") {
alert("Quantity "+ quantity +" for "+productname+" can't be greater than the quantity procured for invoice number "+invoice+" 1");
a = "0";
return false;
}
}
});
}
if (prodid != 'NA' && muc != '' && amt > 0 && rid != '')
{
if (quantity>0){
udata[rid] = {};
udata[rid]['sub_vendor_id'] = subvendorid;
udata[rid]['procurement_vendor_id'] = vendorid;
udata[rid]['product_id'] = prodid;
udata[rid]['quantity'] = quantity;
udata[rid]['measurement_unit'] = muc;
udata[rid]['amount_received'] = amt;
udata[rid]['reason'] = reason;
udata[rid]['vat_percentage'] = vat_percentage;
udata[rid]['total_amount_before_vat'] = total_amount_before_vat;
udata[rid]['vat_charged_in_bill'] = vat_charged_in_bill;
udata[rid]['invoice_number'] = invoice;
}
else{
del_query.push(rid);
}
}
});
if(a =='1'){
$.ajax({
type: "POST",
url: "../model/bulk_procurement_resold_at_lowprice.php",
data: {action:'updatedb',ud:udata,ad:adata,dd:del_query,username:'<?=$gotuser?>'},
success: function(result){
results = JSON.parse(result);
alert('Number of records Updated : '+results['utotal_s']+"\nNumber Of records Inserted : "+results['acnt']+"\nNumber of records Deleted : "+results['dcnt']);
// window.location.href="bulk_procurement_resold_at_lowprice.php?vendorinfo="+vendorid+"&dateinfo="+$("#dateinfo").val()+"&catinfo="+$("#catinfo").val();
}
});
}
这是我的 php 文件
$proid = $_POST['product'];
$code = mysql_real_escape_string($_POST["invno"]);
$vouchdt = mysql_real_escape_string($_POST["date"]);
$qty = mysql_real_escape_string($_POST["quantity"]);
$chkqty = mysql_query("SELECT a.quantity_procured, b.invoice_number FROM `gc_procurement_daily_detail` a, `gc_procurement_daily_summary` b
WHERE a.product_id='".$proid."'
AND b.`date_of_invoice`='".$vouchdt."'
AND b.invoice_number='".$code."'
AND a.`procurement_daily_summary_id`= b.procurement_daily_summary_id")or die(mysql_error());
if(mysql_num_rows($chkqty) > 0){
$gqty =0;
while($row = mysql_fetch_object($chkqty)){
$mqty = $row->quantity_procured;
$gqty = $gqty + $mqty;
}
if($qty <= $gqty){
echo 1;
}else{
echo 2;
}
}else{
echo '';
}