Question

hi there as per my previous question which got solved here i am facing another problem which is that i am using jQuery's $.each() to bind all the dynamically added elements and i want to validate every element as per their order for eg :- 1st req, 2nd req and then 3rd req but here the request is not being sent in their order for eg:- 2nd element , 1st eement, 4th element and then 3rd and this keeps on goin i jus want to know how can i keep these request in their order. here is my whole jQuery code :-

function update_db(){
           var udata = {};
           var adata = {};
           del_query = new Array();
           var confirm = 0;
           var vouchdt     = $("#dateinfo").val();         
           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(prodid !='' && invoice !=''){
        $.ajax({  
             type: "POST",  
             url: "../model/bulk_procurement_resold_at_lowprice.php", 
             data: {para :"upd", invno : invoice, product : prodid, date : vouchdt, quantity : qty},  
             success: function(result){
              if(result == "") {
                 alert(productname+" does not exist for invoice number "+id);
                 $("#savetodb").attr("disabled", "disabled");
              }
              if(result == "2") {
                 alert("Quantity "+ qty +" for "+productname+" can't be greater than the quantity procured for invoice number "+id);
                 $("#savetodb").attr("disabled", "disabled");
              }        
              if(result == "3") {
                 alert("Amount "+amt +" For "+productname+" does not exist for invoice number "+id);
                 $("#savetodb").attr("disabled", "disabled");
              }
           } 
        });

        }


            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(quantity !='' && invoice !=''){

            }           


        });
        // prepare data to be inserted
        var acount = 0;
        $('[id^="tr_"]').each(function(index, table){
                adata[acount]                = {}; 
                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 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");
                   a = "0";
                   console.log(a);
                   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){
                         alert(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;
                             }
                     },
                        dataType: 'script',
                        async: false
                });             
            }

                if (prodid != 'NA' && muc != '' && amt != 0 )
                {
                    if (quantity>0){
                        adata[acount]['procurement_vendor_id'] = vendorid;
                        adata[acount]['product_id'] = prodid;
                        adata[acount]['quantity'] = quantity;
                        adata[acount]['measurement_unit'] = muc;
                        adata[acount]['amount_received'] = amt;
                        adata[acount]['reason'] = reason;
                        adata[acount]['date_of_resold'] = $("#dateinfo").val();
                        adata[acount]['vat_percentage'] = vat_percentage;
                        adata[acount]['total_amount_before_vat'] = total_amount_before_vat;
                        adata[acount]['vat_charged_in_bill'] = vat_charged_in_bill;
                        adata[acount]['sub_vendor_id'] = subvendorid;
                        adata[acount]['invoice_number'] = invoice;

                    }
                     acount++;
                 }

        });

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

    }
    return false;

}
Was it helpful?

Solution

Ajax requests are event driven. Therefore, once a request receives its data, the success: function is called.

You can add the received data to an array as it comes back and then wait until the array is filled ( using if(holderArray.length = x){} or something like that ) with all the returned data to complete your task in order.

Pass the order number along to the success portion in a variable and use that to enter in and then grab the data stored in the array.

holderArray[orderNumber] = result;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top