title: How to insert dynamic data and static input data to mysql using ajax, dynamic data will not insert
body: <p>I have a modal form with static input fields and dynamic input fields.</p>
  
  <p>currently the data is being sent to the mysql database, the static fields are being inserted into the database but the dynamic fields are not being inserted.</p>
  
  <p>The form data has to inserted into three mysql tables. tables are orders, products and sma. </p>
  
  <p><strong>HTML input code</strong> </p>
  
  <pre><code>&lt;div class="modal fade" tabindex="-1" role="dialog" id="insertModal"&gt;
      &lt;div class="modal-dialog" role="document"&gt;
          &lt;div class="modal-content"&gt;
      &lt;form class="well form-horizontal" action="" method="post" id=""&gt;
                  &lt;div class="modal-header"&gt;
                      &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
                      &lt;h3 class="modal-title"&gt;Insert Order Details&lt;/h3&gt;
                  &lt;/div&gt;&lt;hr&gt;
          &lt;fieldset&gt;
  
              &lt;!-- Text input--&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="customer" class="col-md-4 control-label"&gt;Customer Name&lt;/label&gt;
                  &lt;div class="col-md-7 inputGroupContainer"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-user"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;input name="customer" id="customer" placeholder="Customer" class="form-control" type="text"&gt;
                          &lt;label id="lblcustomer" style="color:red"&gt;&lt;/label&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- Text input--&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="date" class="col-md-4 control-label"&gt;Date&lt;/label&gt;
                  &lt;div class="col-md-7 inputGroupContainer"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-calendar"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;input name="date" id="date" placeholder="Date" class="form-control" type="date"&gt;
                          &lt;label id="lbldate" style="color:red"&gt;&lt;/label&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- Text input--&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="invoice" class="col-md-4 control-label"&gt;Invoice&lt;/label&gt;
                  &lt;div class="col-md-7 inputGroupContainer"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-envelope"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;input name="invoice" id="invoice" placeholder="Invoice" class="form-control" type="text"&gt;
                          &lt;label id="lblinvoice" style="color:red"&gt;&lt;/label&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- Text input--&gt;
              &lt;div class="form-group" id="container"&gt;
                  &lt;label for="product" class="col-md-4 control-label"&gt;Product Description&lt;/label&gt;
                  &lt;div class="col-md-7 inputGroupContainer"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-pencil"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;textarea name="product[]" id="product" class="form-control" placeholder="Product Description"&gt;&lt;/textarea&gt;
                          &lt;label id="lblproduct" style="color:red"&gt;&lt;/label&gt;
                      &lt;/div&gt;
                      &lt;a href="#" id="add" style="text-align:center;"&gt;Add More&lt;/a&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- Text input--&gt;
  
              &lt;div class="form-group" id="container1"&gt;
                  &lt;label for="sma_number" class="col-md-4 control-label"&gt;SMA Number&lt;/label&gt;
                  &lt;div class="col-md-7 inputGroupContainer"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-barcode"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;input name="sma_number[]" id="sma_number" placeholder="SMA Number" class="form-control" type="text"&gt;
                          &lt;label id="lblsma_number" style="color:red"&gt;&lt;/label&gt;
                      &lt;/div&gt;
                      &lt;a href="#" id="add1" style="text-align:center;"&gt;Add More&lt;/a&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- Text input--&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="ebay" class="col-md-4 control-label"&gt;eBay Item Number&lt;/label&gt;
                  &lt;div class="col-md-7 inputGroupContainer"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-qrcode"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;input name="ebay" id="ebay" placeholder="eBay Number" class="form-control" type="text"&gt;
                          &lt;label id="lblebay" style="color:red"&gt;&lt;/label&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- Text input--&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="shipper" class="col-md-4 control-label"&gt;Shipped With&lt;/label&gt;
                  &lt;div class="col-md-7 selectContainer"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-road"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;select name="shipper" id="shipper" class="form-control selectpicker"&gt;
                              &lt;option value=" "&gt;Please select Shipping Company&lt;/option&gt;
                              &lt;option&gt;DHL&lt;/option&gt;
                              &lt;option&gt;TNT&lt;/option&gt;
                              &lt;option&gt;FEDEX&lt;/option&gt;
                              &lt;option&gt;AUSPOST&lt;/option&gt;
                              &lt;option&gt;PickUp&lt;/option&gt;
                          &lt;/select&gt;
                          &lt;label id="lblshipper" style="color:red"&gt;&lt;/label&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- Text input--&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="tracking" class="col-md-4 control-label"&gt;Tracking Number&lt;/label&gt;
                  &lt;div class="col-md-7 inputGroupContainer"&gt;
                      &lt;div class="input-group"&gt;
                          &lt;span class="input-group-addon"&gt;&lt;i class="glyphicon glyphicon-home"&gt;&lt;/i&gt;&lt;/span&gt;
                          &lt;input name="tracking" id="tracking" placeholder="Tracking No" class="form-control" type="text"&gt;
                          &lt;label id="lbltracking" style="color:red"&gt;&lt;/label&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/fieldset&gt;
                  &lt;!-- Button --&gt;
                   &lt;div class="modal-footer"&gt;
                      &lt;input type="button" name="save" id="save" value="Save Order" class="btn btn-success"&gt;
                      &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                  &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p><strong>Ajax script</strong></p>
  
  <pre><code>&lt;script&gt;
  
          $(document).ready(function(e){
          //Variables
          var html = '&lt;div class="form-group"&gt;&lt;label class="col-md-4 control-label"&gt;Product&lt;/label&gt;&lt;div class="col-md-6 inputGroupContainer1" style="padding-left: 20px"&gt;&lt;input name="product[]" id="product[]" placeholder="Product" class="form-control" type="text"&gt;&lt;/div&gt;&lt;button id="remove" class="btn btn-danger i_remove_me pull-left" &gt;-&lt;/button&gt;&lt;/div';
          var maxRows = 5;
          var x = 1;
  
          //Add Rows to the form
          $("#add").click(function(e){ 
              e.preventDefault();
              if(x &lt;= maxRows){
              $("#container").append(html);
              x++;
              }
          });
  
          //Remove rows from the from
          $("#container").on('click','#remove', function(e){
              e.preventDefault();
              $(this).parent('div').remove();
              x--;
          });
  
      });
  
          $(document).ready(function(e){
          //Variables
          var html1 = '&lt;div class="form-group"&gt;&lt;label class="col-md-4 control-label"&gt;SMA&lt;/label&gt;&lt;div class="col-md-4 inputGroupContainer1" style="padding-left: 20px"&gt;&lt;input name="sma_number[]" id="sma_number[]" placeholder="SMA Number" class="form-control" type="text"&gt;&lt;/div&gt;&lt;button id="remove" class="btn btn-danger i_remove_me pull-left" &gt;-&lt;/button&gt;&lt;/div';
          var maxRows = 5;
          var x = 1;
  
          //Add Rows to the form
          $("#add1").click(function(e){ 
              e.preventDefault();
              if(x &lt;= maxRows){
              $("#container1").append(html1);
              x++;
              }
          });
  
          //Remove rows from the from
          $("#container1").on('click','#remove', function(e){
              e.preventDefault();
              $(this).parent('div').remove();
              x--;
          });
  
      });
  
  &lt;/script&gt;
  &lt;script&gt;
      //datatable
      $(document).ready(function() {
          $("#emp_table_details").DataTable();
      });
  
  
      $(document).ready(function() {
          //Save Or Insert Data
          $(document).on('click', '#save', function() {
              var customer = $("#customer").val();
              var date = $("#date").val();
              var invoice = $("#invoice").val();
              var product = $("#product").val();
              var sma_number = $("#sma_number").val();
              var ebay = $("#ebay").val();
              var shipper = $("#shipper").val();
              var tracking = $("#tracking").val();
              if (customer == "") {
                  $("#lblcustomer").html("Customer Name Required!");
              } else if (date == "") {
                  $("#lbldate").html("Date Required!");
              } else if (invoice == "") {
                  $("#lblinvoice").html("Invoice Number Required!");
              } else if (product == "") {
                  $("#lblproduct").html("Product Required!");
              } else if (sma_number == "") {
                  $("#lblsma_number").html("SMA Number Required!");
              } else if (ebay == "") {
                  $("#lblebay").html("eBay Number Required!");
              } else if (shipper == "") {
                  $("#lblshipper").html("Shipper Required!");
              } else if (tracking == "") {
                  $("#lbltracking").html("Tracking Required!");
              } else {
                  $.ajax({
                      url: "ajaxsave.php",
                      type: "post",
                      data: {
                          customer: customer,
                          date: date,
                          invoice: invoice,
                          product: product,
                          sma_number: sma_number,
                          ebay: ebay,
                          shipper: shipper,
                          tracking: tracking
                      },
                      success: function() {
                          alert('Your Data Save Sucessful');
                          //document.getElementById('insert_form').reset();
                          //$("#emp_table_details").load('select_data.php');
                          $("#insertModal").modal('hide');
                          location.reload();
                      }
                  });
              }
          });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>PHP insert to MYSQL code</strong></p>
  
  <pre><code>&lt;?php include 'config/config.php'; ?&gt;
  &lt;?php
  global $con;
  //if (isset($_POST)) {
  
      $customer = $_POST['customer'];
      $date = $_POST['date'];
      $invoice = $_POST['invoice'];
      $product = $_POST['product'];
      $sma_number = $_POST['sma_number'];
      $ebay = $_POST['ebay'];
      $shipper = $_POST['shipper'];
      $tracking = $_POST['tracking'];;
  
      $sql = "INSERT INTO orders(customer, date, invoice, ebay, shipper, tracking)
            VALUES('$customer','$date','$invoice','$ebay','$shipper','$tracking')";
  
      $query = mysqli_query($con, $sql);
  
      $newOrderId = mysqli_insert_id($con);
  
      if($query){
      $sql1 = "INSERT INTO products(id, product) VALUES('$newOrderId','$product')";
      $sql2 = "INSERT INTO sma(id, sma_number) VALUES('$newOrderId','$sma_number')";
  
      $result = mysqli_query($con,$sql1);
      $result2 = mysqli_query($con,$sql2);
  
      }
  ?&gt;
  </code></pre>