title: ajax is not working inside for loop
body: <p>i am creating an dynamic invoice stystem... if i select itemcode then it displays item brand of that item code in my brand input id..if i add new item
  then i click on plus button to add new item then i again select a new item code2 then it displays item brand2 of that item code2...so this loop is going on agiain and agian.</p>
  
  <p>my ajax call is not working or some error in ajax code....i cant find the error..if i dint use ajax call only display data of selected var <strong>itemp</strong> then it displays selected data <strong>itemp</strong> in <strong>brand</strong> but using ajax call and find <strong>brand</strong> of selected data <strong>itemp</strong> it didn't works.. please anyone can help me.</p>
  
  <p>here is my javascript ajax call...</p>
  
  <pre><code>function getprods(val) {
          for (j = 1; j &lt;= count; j++) {
              var itemp = $('#item_code' + j).val();
              $.ajax({
                  url: 'ajax_getitems.php',
                  type: 'POST',
                  data: 'state_id=' + itemp,
                  dataType: 'json',
                  success: function (data) {
                      var len = data.length;
                      if (len &gt; 0) {
                          var id = data[0]['id'];
                          var brand = data[0]['brand'];
  
                          document.getElementById('brand' + j).value = brand;
  
  
                      }
  
                  }
              });
          }
      }
  </code></pre>
  
  <p>And here is my ajax_getitems.php file</p>
  
  <pre><code>&lt;?php
  
  include('conn.php');
  $model = $_POST['state_id'];
  $sql = "SELECT * FROM product WHERE code='$model'";
  
  $result = mysqli_query($conn, $sql);
  
  $users_arr = array();
  
  while ($row = mysqli_fetch_array($result)) {
      $id = $row['id'];
      $brand = $row['brand'];
  
      $users_arr[] = array("id"    =&gt; $id,
                           "brand" =&gt; $brand
      );
  }
  
  // encoding array to json format
  echo json_encode($users_arr);
  exit;
  
  ?&gt;  
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/bjNgS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bjNgS.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/2lI3k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2lI3k.png" alt="enter image description here"></a></p>
  
  <p>here is my full html code</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;meta name="robots" content="noindex, nofollow"&gt;
      &lt;link rel="stylesheet" href="css2/bootstrap.min.css"&gt;
      &lt;link rel="stylesheet" type="text/css" href="quotation/jquery-ui.min.css"&gt;
      &lt;script src="js2/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="js2/bootstrap.min.js"&gt;&lt;/script&gt;
      &lt;script src="quotation/js/jquery-ui.js"&gt;&lt;/script&gt;
      &lt;script src="js2/jquery.dataTables.min.js"&gt;&lt;/script&gt;
      &lt;script src="js2/dataTables.bootstrap.min.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="css2/dataTables.bootstrap.min.css"&gt;
      &lt;style&gt;
        /* Remove the navbar's default margin-bottom and rounded borders */ 
        .navbar {
        margin-bottom: 4px;
        border-radius: 0;
        }
        /* Add a gray background color and some padding to the footer */
        footer {
        background-color: #f2f2f2;
        padding: 25px;
        }
        .carousel-inner img {
        width: 100%; /* Set width to 100% */
        margin: auto;
        min-height:200px;
        }
        .navbar-brand
        {
        padding:5px 40px;
        }
        .navbar-brand:hover
        {
        background-color:#ffffff;
        }
        /* Hide the carousel text when the screen is less than 600 pixels wide */
        @media (max-width: 600px) {
        .carousel-caption {
        display: none; 
        }
        }
      &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;style&gt;
        .box
        {
        width: 100%;
        max-width: 1390px;
        border-radius: 5px;
        border:1px solid #ccc;
        padding: 15px;
        margin: 0 auto;                
        margin-top:50px;
        box-sizing:border-box;
        }
      &lt;/style&gt;
      &lt;link rel="stylesheet" href="css2/datepicker.css"&gt;
      &lt;script src="js2/bootstrap-datepicker1.js"&gt;&lt;/script&gt;
      &lt;script&gt;
        $(document).ready(function(){
          $('#order_date').datepicker({
            format: "yyyy-mm-dd",
            autoclose: true
          });
        });
      &lt;/script&gt;
      &lt;script&gt;
      $(document).ready(function(){
  
              $(document).on('keydown', '.name', function() {
  
                  var id = this.id;
                  var splitid = id.split('_');
                  var index = splitid[1];
  
                  $( '#'+id ).autocomplete({
                      source: function( request, response ) {
                          $.ajax({
                              url: "getDetails.php",
                              type: 'post',
                              dataType: "json",
                              data: {
                                  search: request.term,request:1
                              },
                              success: function( data ) {
                                  response( data );
                              }
                          });
                      },
                      select: function (event, ui) {
                          $(this).val(ui.item.label); // display the selected text
                          var userid = ui.item.value; // selected id to input
  
                          // AJAX
                          $.ajax({
                              url: 'getDetails.php',
                              type: 'post',
                              data: {userid:userid,request:2},
                              dataType: 'json',
                              success:function(response){
  
                                  var len = response.length;
  
                                  if(len &gt; 0){
                                      var id = response[0]['id'];
                                      var phone = response[0]['phone'];
                                      var email = response[0]['email'];
                                      var address = response[0]['address'];
                                      var gst = response[0]['gst'];
  
                                      document.getElementById('phone').value = phone;
                                      document.getElementById('email').value = email;
                                      document.getElementById('address').value = address;
                                      document.getElementById('gst').value = gst;
  
                                  }
  
                              }
                          });
  
                          return false;
                      }
                  });
              });
  
  
          });
  
  function getproduct(val) {
  $.ajax({
  url: 'ajax_getproduct.php',
  type: 'POST',
  data: 'state_id='+val,
  dataType: 'json',
  success:function(data){
  var len = data.length;
  if(len &gt; 0){
  var id = data[0]['id'];
  var item = data[0]['item'];
  var subcat = data[0]['subcat'];
  var brand = data[0]['brand'];
  var model = data[0]['model'];
  var specification = data[0]['specification'];
  var unitrate = data[0]['unitrate'];
  var unitmesure = data[0]['unitmesure'];
  
  document.getElementById('item').value = item;
  document.getElementById('subcat').value = subcat;
  document.getElementById('brand').value = brand;
  document.getElementById('model').value = model;
  document.getElementById('Specification').value = specification;
  document.getElementById('unitrate1').value = unitrate;
  document.getElementById('unitmesure').value = unitmesure;
  
  }
  }
  });
  }
  &lt;/script&gt;
      &lt;div class="container-fluid"&gt;
  
        &lt;form method="post" id="invoice_form"&gt;
          &lt;div class="table-responsive"&gt;
            &lt;table class="table table-bordered"&gt;
              &lt;tr&gt;
                &lt;td colspan="2" align="center"&gt;&lt;h2 style="margin-top:10.5px"&gt;Create Quotation&lt;/h2&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td colspan="2"&gt;
                    &lt;div class="row"&gt;
                      &lt;div class="col-md-8"&gt;
                        To,&lt;br /&gt;
                          &lt;b&gt;RECEIVER (BILL TO)&lt;/b&gt;&lt;br /&gt;
                          &lt;input type="text" name="name" id="name" class="form-control name" placeholder="Enter Receiver Name" /&gt;
                          &lt;textarea name="address" id="address" class="form-control" placeholder="Enter Billing Address"&gt;&lt;/textarea&gt;
                      &lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                        &lt;br /&gt;
                        &lt;input type="text" name="phone" id="phone" class="form-control input-sm" placeholder="Phone No." /&gt;
                        &lt;input type="text" name="email" id="email" class="form-control input-sm" placeholder="Email" /&gt;
                        &lt;input type="text" name="gst" id="gst" class="form-control input-sm" placeholder="GST" /&gt;
                      &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;br /&gt;
                    &lt;table id="invoice-item-table" class="table table-bordered"&gt;
                      &lt;tr&gt;
                        &lt;th&gt;Sr No.&lt;/th&gt;
                        &lt;th&gt;Item Code&lt;/th&gt;
                                  &lt;th&gt;Brand&lt;/th&gt;
                        &lt;th&gt;Model&lt;/th&gt;
                        &lt;th&gt;Specification&lt;/th&gt;
                        &lt;th&gt;Unit Rate&lt;/th&gt;
                        &lt;th&gt;Quantity&lt;/th&gt;
  
                        &lt;th&gt;Actual Amt.&lt;/th&gt;
                        &lt;th colspan="2"&gt;Discount (%)&lt;/th&gt;
  
                        &lt;th rowspan="2"&gt;Total&lt;/th&gt;
                        &lt;th rowspan="2"&gt;&lt;/th&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                        &lt;th&gt;&lt;/th&gt;
                        &lt;th&gt;&lt;/th&gt;
                        &lt;th&gt;&lt;/th&gt;
  
                        &lt;th&gt;&lt;/th&gt;
                        &lt;th&gt;&lt;/th&gt;
                        &lt;th&gt;Rate&lt;/th&gt;
                        &lt;th&gt;&lt;/th&gt;
                        &lt;th&gt;Amt.&lt;/th&gt;
  
                      &lt;/tr&gt;
                      &lt;tr&gt;
                        &lt;td&gt;&lt;span id="sr_no"&gt;1&lt;/span&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;select type="text" name="item_code[]" id="item_code" class="form-control input-sm"  onChange="getproduct(this.value);" &gt;
                        &lt;option value=""&gt;&lt;/option&gt;
                        &lt;?php
      include "conn.php";
      $sql = "SELECT * FROM `product`";
      $run = mysqli_query($conn,$sql);
  
      while($row=mysqli_fetch_array($run))
      {
      ?&gt;
  &lt;option&gt;&lt;?php echo $row['code']; ?&gt;&lt;/option&gt;
          &lt;?php } ?&gt;
                        &lt;/select&gt;
              &lt;input type="hidden" name="item[]" id="item"&gt;
               &lt;input type="hidden" name="subcat[]" id="subcat"&gt;
                        &lt;/td&gt;
                        &lt;td&gt;
                            &lt;input type="text" name="brand[]" id="brand" class="form-control input-sm" readonly&gt; 
                        &lt;/td&gt;
  
              &lt;td&gt;
                &lt;input type="text" name="model[]" id="model" class="form-control input-sm" readonly&gt;
              &lt;/td&gt;
              &lt;td&gt;
                &lt;textarea name="Specification[]" id="Specification" class="form-control" readonly&gt;&lt;/textarea&gt;
              &lt;/td&gt;
                 &lt;td&gt;&lt;input type="text" name="unitrate[]" id="unitrate1" data-srno="1" class="form-control input-sm unitrate" /&gt;
                  &lt;input type="hidden" name="unitmesure" id="unitmesure"&gt;
                 &lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="qty[]" id="qty1" data-srno="1" class="form-control input-sm number_only qty" /&gt;&lt;/td&gt;
  
                        &lt;td&gt;&lt;input type="text" name="order_item_actual_amount[]" id="order_item_actual_amount1" data-srno="1" class="form-control input-sm order_item_actual_amount" readonly /&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="order_item_tax1_rate[]" id="order_item_tax1_rate1" data-srno="1" class="form-control input-sm number_only order_item_tax1_rate" /&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="order_item_tax1_amount[]" id="order_item_tax1_amount1" data-srno="1" readonly class="form-control input-sm order_item_tax1_amount" /&gt;&lt;/td&gt;
                       &lt;td&gt;&lt;input type="text" name="order_item_final_amount[]" id="order_item_final_amount1" data-srno="1" readonly class="form-control input-sm order_item_final_amount" /&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                    &lt;/table&gt;
                    &lt;div align="right"&gt;
                      &lt;button type="button" name="add_row" id="add_row" class="btn btn-success btn-xs"&gt;+&lt;/button&gt;
                    &lt;/div&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td align="right"&gt;&lt;b&gt;Total&lt;/td&gt;
                  &lt;td align="right"&gt;&lt;b&gt;&lt;span id="final_total_amt"&gt;&lt;/span&gt;&lt;/b&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td colspan="2"&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td colspan="2" align="center"&gt;
                    &lt;input type="hidden" name="total_item" id="total_item" value="1" /&gt;
                    &lt;input type="submit" name="create_invoice" id="create_invoice" class="btn btn-info" value="Create" /&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
  
                &lt;tr&gt;
  
                &lt;/tr&gt;
            &lt;/table&gt;
          &lt;/div&gt;
        &lt;/form&gt;
        &lt;script&gt;
  
        $(document).ready(function(){
          var final_total_amt = $('#final_total_amt').text();
          var count = 1;
  
  
          $(document).on('click', '#add_row', function(){
            count++;
  
            $('#total_item').val(count);
            var html_code = '';
            html_code += '&lt;tr id="row_id_'+count+'"&gt;';
            html_code += '&lt;td&gt;&lt;span id="sr_no"&gt;'+count+'&lt;/span&gt;&lt;/td&gt;';
  
           html_code += '&lt;td&gt;&lt;select name="item_code[]" id="item_code'+count+'" class="form-control input-sm item_code"&gt;&lt;option&gt;&lt;/option&gt;&lt;?php $sql='Select * from product'; $run=mysqli_query($conn,$sql); while($row=mysqli_fetch_array($run)){ echo '&lt;option&gt;'.$row['code'].'&lt;/option&gt;'; } ?&gt;&lt;/select&gt;&lt;input type="hidden" name="item[]" id="item'+count+'"&gt;&lt;input type="hidden" name="subcat[]" id="subcat'+count+'"&gt;&lt;/td&gt;';
  
  
            html_code += '&lt;td&gt;&lt;input type="text" name="brand[]" id="brand'+count+'" class="form-control input-sm" readonly&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;input type="text" name="model[]" id="model'+count+'" class="form-control input-sm" readonly&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;textarea name="Specification[]" id="Specification'+count+'" class="form-control" readonly&gt;&lt;/textarea&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;input type="text" name="unitrate[]" id="unitrate'+count+'" data-srno="'+count+'" class="form-control input-sm number_only unitrate" /&gt;&lt;input type="hidden" name="unitmesure[]" id="unitmesure'+count+'"&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;input type="text" name="qty[]" id="qty'+count+'" data-srno="'+count+'" class="form-control input-sm number_only qty" /&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;input type="text" name="order_item_actual_amount[]" id="order_item_actual_amount'+count+'" data-srno="'+count+'" class="form-control input-sm order_item_actual_amount" readonly /&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;input type="text" name="order_item_tax1_rate[]" id="order_item_tax1_rate'+count+'" data-srno="'+count+'" class="form-control input-sm number_only order_item_tax1_rate" /&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;input type="text" name="order_item_tax1_amount[]" id="order_item_tax1_amount'+count+'" data-srno="'+count+'" readonly class="form-control input-sm order_item_tax1_amount" /&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;input type="text" name="order_item_final_amount[]" id="order_item_final_amount'+count+'" data-srno="'+count+'" readonly class="form-control input-sm order_item_final_amount" /&gt;&lt;/td&gt;';
  
            html_code += '&lt;td&gt;&lt;button type="button" name="remove_row" id="'+count+'" class="btn btn-danger btn-xs remove_row"&gt;X&lt;/button&gt;&lt;/td&gt;';
            html_code += '&lt;/tr&gt;';
            $('#invoice-item-table').append(html_code);
          });
  
          $(document).on('click', '.remove_row', function(){
            var row_id = $(this).attr("id");
            var total_item_amount = $('#order_item_final_amount'+row_id).val();
            var final_amount = $('#final_total_amt').text();
            var result_amount = parseFloat(final_amount) - parseFloat(total_item_amount);
            $('#final_total_amt').text(result_amount);
            $('#row_id_'+row_id).remove();
            count--;
            $('#total_item').val(count);
          });
  
  
  
  
  
          function cal_final_total(count)
          {
            var final_item_total = 0;
            for(j=1; j&lt;=count; j++)
            {
              var quantity = 0;
              var price = 0;
              var actual_amount = 0;
              var tax1_rate = 0;
              var tax1_amount = 0;
              var tax2_rate = 0;
              var tax2_amount = 0;
              var tax3_rate = 0;
              var tax3_amount = 0;
              var item_total = 0;
              quantity = $('#unitrate'+j).val();
              if(quantity &gt; 0)
              {
                price = $('#qty'+j).val();
                if(price &gt; 0)
                {
                  actual_amount = parseFloat(quantity) * parseFloat(price);
                  $('#order_item_actual_amount'+j).val(actual_amount);
                  tax1_rate = $('#order_item_tax1_rate'+j).val();
                  if(tax1_rate &gt; 0)
                  {
                    tax1_amount = -(parseFloat(actual_amount)*parseFloat(tax1_rate)/100);
                    $('#order_item_tax1_amount'+j).val(tax1_amount);
                  }
  
                  item_total = parseFloat(actual_amount) + parseFloat(tax1_amount) + parseFloat(tax2_amount) + parseFloat(tax3_amount);
                  final_item_total = parseFloat(final_item_total) + parseFloat(item_total);
                  $('#order_item_final_amount'+j).val(item_total);
                }
              }
            }
            $('#final_total_amt').text(final_item_total);
          }
  
  function getprods()
  {
   for(j=1; j&lt;=count; j++)
  {
  var itemp = $('#item_code'+j).val();
  $.ajax({
  url: 'ajax_getitems.php',
  type: 'POST',
  data: 'state_id='+itemp,
  dataType: 'json',
  success:function(data){
  var len = data.length;
  if(len &gt; 0){
  var id = data[0]['id'];
  var brand = data[0]['brand'];
  
  document.getElementById('brand'+j).value = brand;
  
  
  }
  
  }
  });
  }
  }
  
  
          $(document).on('blur', '.qty', function(){
            cal_final_total(count);
          });
  
          $(document).on('blur', '.order_item_tax1_rate', function(){
            cal_final_total(count);
          });
  
           $(document).on('change', '.item_code', function(){
                getprods(this.value);
          });
  
  
  
          $('#create_invoice').click(function(){
            if($.trim($('#order_receiver_name').val()).length == 0)
            {
              alert("Please Enter Reciever Name");
              return false;
            }
  
            if($.trim($('#order_no').val()).length == 0)
            {
              alert("Please Enter Invoice Number");
              return false;
            }
  
            if($.trim($('#order_date').val()).length == 0)
            {
              alert("Please Select Invoice Date");
              return false;
            }
  
            for(var no=1; no&lt;=count; no++)
            {
              if($.trim($('#item_name'+no).val()).length == 0)
              {
                alert("Please Enter Item Name");
                $('#item_name'+no).focus();
                return false;
              }
  
              if($.trim($('#unitrate'+no).val()).length == 0)
              {
                alert("Please Enter Quantity");
                $('#unitrate'+no).focus();
                return false;
              }
  
              if($.trim($('#qty'+no).val()).length == 0)
              {
                alert("Please Enter Price");
                $('#qty'+no).focus();
                return false;
              }
  
            }
  
            $('#invoice_form').submit();
  
          });
  
        });
        &lt;/script&gt;
  
  
  
  &lt;script&gt;
  $(document).ready(function(){
  $('.number_only').keypress(function(e){
  return isNumbers(e, this);      
  });
  function isNumbers(evt, element) 
  {
  var charCode = (evt.which) ? evt.which : event.keyCode;
  if (
  (charCode != 46 || $(element).val().indexOf('.') != -1) &amp;&amp;      // “.” CHECK DOT, AND ONLY ONE.
  (charCode &lt; 48 || charCode &gt; 57))
  return false;
  return true;
  }
  });
  
  
  &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  
  
  &lt;?php
  include('conn2.php');
  if(isset($_POST['create_invoice']))
  {
    $order_receiver_name=$_POST['order_receiver_name'];
    $order_receiver_address=$_POST['order_receiver_address'];
    $order_no=$_POST['order_no'];
    $order_date=$_POST['order_date'];
  
    $order_total_before_tax = 0;
    $order_total_tax1 = 0;
    $order_total_tax2 = 0;
    $order_total_tax3 = 0;
    $order_total_tax = 0;
    $order_total_after_tax = 0;
  
    date_default_timezone_set('Asia/Kolkata');
      $current = date('d-m-Y H:i:s');
  
    $sql = "INSERT INTO `tbl_order`(`order_no`, `order_date`, `order_receiver_name`, `order_receiver_address`, `order_total_before_tax`, `order_total_tax1`, `order_total_tax2`, `order_total_tax3`, `order_total_tax`, `order_total_after_tax`, `order_datetime`) VALUES ('$order_no','$order_date','$order_receiver_name','$order_receiver_address','$order_total_before_tax','$order_total_tax1','$order_total_tax2','$order_total_tax3','$order_total_tax','$order_total_after_tax','$current')";
  
    $run = mysqli_query($conn, $sql);
  
    if($run)
  
        {
            echo "&lt;script&gt;alert('Inserted Successfully')&lt;/script&gt;";
        }
        else{
            echo("Error description: " . mysqli_error($conn));
        }
  
   $last_id = mysqli_insert_id($conn);
  
    for($count=0; $count&lt;$_POST["total_item"]; $count++)
    {
      $item_name =  $_POST['item_name'][$count];
      $unitrate =  $_POST['unitrate'][$count];
      $qty =  $_POST['qty'][$count];
      $order_item_actual_amount =  $_POST['order_item_actual_amount'][$count];
      $order_item_tax1_rate =  $_POST['order_item_tax1_rate'][$count];
      $order_item_tax1_amount =  $_POST['order_item_tax1_amount'][$count];
      $order_item_tax2_rate =  $_POST['order_item_tax2_rate'][$count];
      $order_item_tax2_amount =  $_POST['order_item_tax2_amount'][$count];
      $order_item_tax3_rate =  $_POST['order_item_tax3_rate'][$count];
      $order_item_tax3_amount =  $_POST['order_item_tax3_amount'][$count];
      $order_item_final_amount =  $_POST['order_item_final_amount'][$count];
  
      $sql2 = "INSERT INTO `tbl_order_item`(`order_id`, `item_name`, `order_item_quantity`, `order_item_price`, `order_item_actual_amount`, `order_item_tax1_rate`, `order_item_tax1_amount`, `order_item_tax2_rate`, `order_item_tax2_amount`, `order_item_tax3_rate`, `order_item_tax3_amount`, `order_item_final_amount`) VALUES ('$last_id','$item_name','$order_item_quantity','$order_item_price','$order_item_actual_amount','$order_item_tax1_rate','$order_item_tax1_amount','$order_item_tax2_rate','$order_item_tax2_amount','$order_item_tax3_rate','$order_item_tax3_amount','$order_item_final_amount')";
  
      $run2 = mysqli_query($conn, $sql2);
  
    if($run2)
  
      {
        echo "&lt;script&gt;alert('inserted successfully')&lt;/script&gt;";
      }
      else{
        echo("Error description: " . mysqli_error($conn));
      }
  
      $order_total_before_tax = $order_total_before_tax + floatval(trim($_POST["order_item_actual_amount"][$count]));
  
          $order_total_tax1 = $order_total_tax1 + floatval(trim($_POST["order_item_tax1_amount"][$count]));
  
          $order_total_tax2 = $order_total_tax2 + floatval(trim($_POST["order_item_tax2_amount"][$count]));
  
          $order_total_tax3 = $order_total_tax3 + floatval(trim($_POST["order_item_tax3_amount"][$count]));
  
          $order_total_after_tax = $order_total_after_tax + floatval(trim($_POST["order_item_final_amount"][$count]));
  
  
  
    }
    $order_total_tax = $order_total_tax1 + $order_total_tax2 + $order_total_tax3;
  
    $sql3 = "UPDATE `tbl_order` SET `order_total_before_tax`='$order_total_before_tax',`order_total_tax1`='$order_total_tax1',`order_total_tax2`='$order_total_tax2',`order_total_tax3`='$order_total_tax3',`order_total_tax`='$order_total_tax',`order_total_after_tax`='$order_total_after_tax' WHERE `order_id`='$last_id'";
  
    $run3 = mysqli_query($conn, $sql3);
  
    if($run3)
  
      {
        echo "&lt;script&gt;alert('inserted successfully')&lt;/script&gt;";
      }
      else{
        echo("Error description: " . mysqli_error($conn));
      }
  }
  
  
  ?&gt;
  </code></pre>