简体   繁体   中英

What's the issue in getting the values from dynamically appended table row fields in post array after submitting the form?

I've an HTML form. This form is containing HTML table. The actual HTML table is very large. For your reference I'm showing below the HTML code of a form with table containing only two rows:

<form action="rebates.php" role="form" method="post">
  <div style="margin-left: 12px" class="col-xs-4">
    <div class="form-group">
      <label for="company_name" class="col-lg-4">Manufacturer </label>
        <div class="col-lg-7">
          <select id="company_name" name="company_name" class="form-control">
            <option value=""  selected='selected'>Select Manufacturer</option>
            <option value="33" >Eywa Solutions</option>
            <option value="37" >Amazon</option>
            <option value="40" >Test</option>
            <option value="42" >RK</option>
            <option value="46" >Santa Margherita</option>
          </select>
        </div>
      </div>
    </div>
    <div style="margin-left: -61px" class="col-xs-4">
      <div class="form-group">
        <label for="product_id" class="col-lg-3">Product </label>
          <div class="col-lg-7">
            <select id="product_id" name="product_id" class="form-control">
              <option value=""  selected='selected'>Select Product</option>
              <option value="5" >Chesse</option>
              <option value="8" >Laptop an</option>
              <option value="9" >Prosecco</option>
            </select>
          </div>
        </div>
      </div>
    </div>
    <br/> 
    <div class="col-lg-2"></div>            
      <div class="col-md-8">   
        <div style="overflow:auto" class="well">      
          <button style="float:right; margin-bottom: 20px" class="btnAdd" type="button" class="btn btn-default"><i class="icon-plus"></i> &nbsp;Add New Rebate</button>
          <br/>
          <div class="table-responsive">
            <table   id="blacklistgrid"  class="table table-bordered table-hover table-striped">
              <thead>
                <tr  id="Row1">
                  <th style="vertical-align:middle" >Pack Of</th>
                  <th style="vertical-align:middle">Quantity</th>
                  <th style="vertical-align:middle">Volume</th>
                  <th style="vertical-align:middle">Unit</th>
                  <th style="vertical-align:middle">Rebate Amount</th>
                </tr>
              </thead>
              <tbody>
                <tr id="Row2">
                  <td><input type="text" name="pack[]" value="" class="form-control" size="8"/></td>
                  <td><input type="text" name="quantity[]" value="2" class="form-control" size="8"/></td>
                  <td><input type="text" name="volume[]" value="750" class="form-control" size="8"/></td>
                  <td>
                    <div class="btn-group">
                      <select name="units[]" class="form-control">
                        <option value=""  selected='selected'>Select Unit</option>
                        <option value="5" >Microsecond</option>
                        <option value="7" >oz</option>
                        <option value="9" >ml</option>
                        <option value="10" >L</option>
                        <option value="12" >gms</option>
                      </select>
                    </div>
                  </td>
                  <td>
                    <input type="text" name="amount[]" value="3.00" class="form-control" size="9"/>
                  </td>
                </tr>            
                <tr>
                  <td><input type="text" name="pack[]" value="" class="form-control" size="8"/></td>
                  <td><input type="text" name="quantity[]" value="4" class="form-control" size="8"/></td>
                  <td><input type="text" name="volume[]" value="750" class="form-control" size="8"/></td>
                  <td>
                    <div class="btn-group">
                      <select name="units[]" class="form-control">
                        <option value=""  selected='selected'>Select Unit</option>
                        <option value="5" >Microsecond</option>
                        <option value="7" >oz</option>
                        <option value="9" >ml</option>
                        <option value="10" >L</option>
                        <option value="12" >gms</option>
                      </select>
                    </div>
                  </td>
                  <td><input type="text" name="amount[]" value="7.00" class="form-control" size="9"/></td>
                </tr>                      
              </tbody>
            </table>
            <button style="float:right" class="btnAdd" type="button" class="btn btn-default"><i class="icon-plus"></i> &nbsp;Add New Rebate</button>
          </div>
        </div> <!-- /span8 -->    
        <div class="row">
          <div  class="col-xs-5"></div>
          <div style="margin-left: -9px"  class="col-xs-5">
            <button type="submit" class="btn btn-primary">Preview</button>
          </div>                
        </div>
      </div>
    </form>

I'm dynamically appending rows to the table by clicking on a button(the button is present in a tag, you can see in above code). The various JQuery code snippets I tried for adding rows dynamically are as follows. All are working:

/*JQuery for appending rows at the end of table*/
<script language="javascript"> 
$( document ).ready(function() {
  $('.btnAdd').click(function () {
    var count = 1,
    first_row = $('#Row2');
    //while (count-- > 0) first_row.clone().appendTo('#blacklistgrid');
      while (count-- > 0) first_row.clone().removeAttr('id').appendTo('#blacklistgrid');
    //while (count-- > 0) $('#blacklistgrid > tbody:last').append(first_row.clone().removeAttr('id'));
    //while (count-- > 0) first_row.clone().appendTo('#blacklistgrid').attr('id','Row' + $('#blacklistgrid tr').length);
  });
});
</script>

From above code snippets you can suggest me the most optimum one. Now the issue I'm facing is if I append one or more rows at the end of table, fill data in the textfields of each appended row and submit the form by clicking on Submit button, in $_POST on rebates.php I'm not getting the data from appended rows. I'm getting the data only from the rows which are previously present when the page loads. So can anyone help me in getting the values from the dynamically appended rows also? Thanks for spending some of your valuable time in understanding my issue. Waiting for your precious replies. Still, if you need any further information regarding the question I can provide you the same. I'm using following jQuery libraries:

    <script src="http://localhost/smart-rebate-web/web/js/libs/jquery-1.9.1.min.js"></script>
    <script src="http://localhost/smart-rebate-web/web/js/libs/jquery-ui-1.10.0.custom.min.js"></script>
    <script src="http://localhost/smart-rebate-web/web/js/libs/bootstrap.min.js"></script>

    <script src="http://localhost/smart-rebate-web/web/js/plugins/validate/jquery.validate.js"></script>

    <script src="http://localhost/smart-rebate-web/web/js/Application.js"></script>
    <script src="http://localhost/smart-rebate-web/web/js/demo/validation.js"></script>

Here is what i tested and works fine:

the .html file :

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$( document ).ready(function() {

  var first_row = $('#Row2').clone();
  $('.btnAdd').click(function () {
    var count = 1;

    first_row.clone().removeAttr('id').appendTo('#blacklistgrid');

  });
});

</script>
</head>
<body>
<form action="formdata.php" role="form" method="post">
  <div style="margin-left: 12px" class="col-xs-4">
    <div class="form-group">
      <label for="company_name" class="col-lg-4">Manufacturer </label>
        <div class="col-lg-7">
          <select id="company_name" name="company_name" class="form-control">
            <option value=""  selected='selected'>Select Manufacturer</option>
            <option value="33" >Eywa Solutions</option>
            <option value="37" >Amazon</option>
            <option value="40" >Test</option>
            <option value="42" >RK</option>
            <option value="46" >Santa Margherita</option>
          </select>
        </div>
      </div>
    </div>
    <div style="margin-left: -61px" class="col-xs-4">
      <div class="form-group">
        <label for="product_id" class="col-lg-3">Product </label>
          <div class="col-lg-7">
            <select id="product_id" name="product_id" class="form-control">
              <option value=""  selected='selected'>Select Product</option>
              <option value="5" >Chesse</option>
              <option value="8" >Laptop an</option>
              <option value="9" >Prosecco</option>
            </select>
          </div>
        </div>
      </div>
    </div>
    <br/> 
    <div class="col-lg-2"></div>            
      <div class="col-md-8">   
        <div style="overflow:auto" class="well">      
          <button style="float:right; margin-bottom: 20px" class="btnAdd" type="button" class="btn btn-default"><i class="icon-plus"></i> &nbsp;Add New Rebate</button>
          <br/>
          <div class="table-responsive">
            <table   id="blacklistgrid"  class="table table-bordered table-hover table-striped">
              <thead>
                <tr  id="Row1">
                  <th style="vertical-align:middle" >Pack Of</th>
                  <th style="vertical-align:middle">Quantity</th>
                  <th style="vertical-align:middle">Volume</th>
                  <th style="vertical-align:middle">Unit</th>
                  <th style="vertical-align:middle">Rebate Amount</th>
                </tr>
              </thead>
              <tbody>
                <tr id="Row2">
                  <td><input type="text" name="pack[]" value="" class="form-control" size="8"/></td>
                  <td><input type="text" name="quantity[]" value="2" class="form-control" size="8"/></td>
                  <td><input type="text" name="volume[]" value="750" class="form-control" size="8"/></td>
                  <td>
                    <div class="btn-group">
                      <select name="units[]" class="form-control">
                        <option value=""  selected='selected'>Select Unit</option>
                        <option value="5" >Microsecond</option>
                        <option value="7" >oz</option>
                        <option value="9" >ml</option>
                        <option value="10" >L</option>
                        <option value="12" >gms</option>
                      </select>
                    </div>
                  </td>
                  <td>
                    <input type="text" name="amount[]" value="3.00" class="form-control" size="9"/>
                  </td>
                </tr>            
                <tr>
                  <td><input type="text" name="pack[]" value="" class="form-control" size="8"/></td>
                  <td><input type="text" name="quantity[]" value="4" class="form-control" size="8"/></td>
                  <td><input type="text" name="volume[]" value="750" class="form-control" size="8"/></td>
                  <td>
                    <div class="btn-group">
                      <select name="units[]" class="form-control">
                        <option value=""  selected='selected'>Select Unit</option>
                        <option value="5" >Microsecond</option>
                        <option value="7" >oz</option>
                        <option value="9" >ml</option>
                        <option value="10" >L</option>
                        <option value="12" >gms</option>
                      </select>
                    </div>
                  </td>
                  <td><input type="text" name="amount[]" value="7.00" class="form-control" size="9"/></td>
                </tr>                      
              </tbody>
            </table>
            <button style="float:right" class="btnAdd" type="button" class="btn btn-default"><i class="icon-plus"></i> &nbsp;Add New Rebate</button>
          </div>
        </div> <!-- /span8 -->    
        <div class="row">
          <div  class="col-xs-5"></div>
          <div style="margin-left: -9px"  class="col-xs-5">
            <button type="submit" class="btn btn-primary">Preview</button>
          </div>                
        </div>
      </div>
    </form>
</body>
</html>

formdata.php

<?php
print_r($_POST);
?>

Sample data returned :

    Array
(
    [company_name] => 
    [product_id] => 5
    [pack] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [quantity] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 2
            [3] => 2
        )

    [volume] => Array
        (
            [0] => 750
            [1] => 750
            [2] => 750
            [3] => 750
        )

    [units] => Array
        (
            [0] => 7
            [1] => 5
            [2] => 7
            [3] => 7
        )

    [amount] => Array
        (
            [0] => 3.00
            [1] => 7.00
            [2] => 3.00
            [3] => 3.00
        )

)

As you can see, it adds all the informations you are appending with jQuery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM