问题
I am working with a form which consist of dynamic values like adding table rows on button click and I am doing this with jQuery, but now I even want to validate my data before inserting it into my database.
I had to do this validation with external PHP file I am doing this with AJAX, but the validation is working well but still the data is being inserted into my database. I have tried my best but still its not working for me.
Here is my code:
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();
}
});
}
here is my php file
$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 '';
}
回答1:
This is because you are doing two ajax calls and the second if statement is being hit before the value of a is changed. How the code will be read is:
var a = "1";
if ($("#maintable tbody tr").length>0){
//some code here it goes through in order until:
$.ajax({
//do this in the background, meanwhile keep executing the code on the page.
});
}
a is still 1 because results have not come back yet from previous ajax call.
if(a =='1'){
$.ajax({
//now do this
});
}
What you want to do is nest that second ajax call in the success of the first, e.g.:
$.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 ==1){
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();
}
});
} else {
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;
}
}
}
});
And you'll need to move the if (prodid != 'NA' && muc != '' && amt > 0 && rid != '')
function above the ajax call as well.
来源:https://stackoverflow.com/questions/19588849/validating-dynamically-added-table-row-values-from-external-php-in-ajax