Note that there are some explanatory texts on larger screens.

plurals
  1. POValidating dynamically added table row values from external php in ajax
    text
    copied!<p>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. </p> <p>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.</p> <p>Here is my code:</p> <pre><code>function update_db(){ var udata = {}; var adata = {}; del_query = new Array(); var confirm = 0; var a = "1"; if ($("#maintable tbody tr").length&gt;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 =='' &amp;&amp; prodid !=''){ alert("Invoice Number Cannot Be Empty"); $("#savetodb").prop("disabled", true); a = "0"; return false; } if( quantity !='' &amp;&amp; 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' &amp;&amp; muc != '' &amp;&amp; amt &gt; 0 &amp;&amp; rid != '') { if (quantity&gt;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:'&lt;?=$gotuser?&gt;'}, 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+"&amp;dateinfo="+$("#dateinfo").val()+"&amp;catinfo="+$("#catinfo").val(); } }); } </code></pre> <p>here is my php file</p> <pre><code>$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) &gt; 0){ $gqty =0; while($row = mysql_fetch_object($chkqty)){ $mqty = $row-&gt;quantity_procured; $gqty = $gqty + $mqty; } if($qty &lt;= $gqty){ echo 1; }else{ echo 2; } }else{ echo ''; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload