简体   繁体   English

提交表单后,如何使用JQuery从附加到HTML表的字段中获取$ _POST数组中的值?

[英]How to get the values in $_POST array from the fields which are appended to the HTML table using JQuery after form submission?

I've one HTML form. 我有一个HTML表单。 This form is containing HTML table. 此表单包含HTML表。 The actual HTML table is very large. 实际的HTML表非常大。 For your reference I'm showing below the HTML code of a form with tablw containing only two records: 供您参考,我在下面显示带有tablw的表单的HTML代码,该表单仅包含两个记录:

<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">
                  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                  Units
                  <span class="caret"></span>
                  </button>
                  <ul class="dropdown-menu">
                    <li><a href="#">Microsecond</a></li>
                    <li><a href="#">oz</a></li>
                    <li><a href="#">ml</a></li>
                    <li><a href="#">L</a></li>
                    <li><a href="#">gms</a></li>
                  </ul>
                </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="button" class="btn btn-default">Go Back</button>
        <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 jQuery code I writen for adding rows dynamically is as follows: 我为动态添加行而编写的jQuery代码如下:

/*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');
  });
});
</script>

Now the issue I'm facing is if I append one or more rows at the end of table, fill data in the textfields from each appended row and submit the form by clicking on Submit button, in $_POST I'm not able to get the data from appended rows. 现在我面临的问题是,如果我在表的末尾追加一个或多个行,在每个追加行的文本字段中填充数据,然后通过单击“提交”按钮提交表单,在$_POST我无法获得附加行中的数据。 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? 那么,有人可以帮助我从动态附加的行中获取值吗?

The HTMl code of dynamically appended table row is as follows: 动态附加表行的HTMl代码如下:

<tr>
  <td>
    <input class="form-control" type="text" size="8" value="" name="pack[]">
  </td>
  <td>
    <input class="form-control" type="text" size="8" value="2" name="quantity[]">
  </td>
  <td>
    <input class="form-control" type="text" size="8" value="750" name="volume[]">
  </td>
  <td>
    <div class="btn-group">
      <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button">
      Units
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu">
        <li>
          <a href="#">Microsecond</a>
        </li>
        <li>
          <a href="#">oz</a>
        </li>
        <li>
          <a href="#">ml</a>
        </li>
        <li>
          <a href="#">L</a>
        </li>
        <li>
          <a href="#">gms</a>
        </li>
      </ul>
    </div>
  </td>
  <td>
    <input class="form-control" type="text" size="9" value="3.00" name="amount[]">
  </td>
</tr>

Well, you have done the right thing. 好吧,你做对了。 Please see the name field of your input element. 请查看输入元素的名称字段。

<input type="text" name="pack[]" value="" class="form-control" size="8"/>

Please notice the name="pack[]" using an array of name as input. 请注意name="pack[]"使用名称数组作为输入。 If you have cloned the table, the input element will be cloned. 如果您已经克隆了表,那么输入元素也将被克隆。 Since you aren't using an id, it's not a problem. 由于您没有使用ID,所以这不是问题。

Now, when sending the data, in rebates.php check if the form data is present. 现在,在发送数据时,在rebates.php检查表单数据是否存在。 It can be done by checking up the request method. 可以通过检查请求方法来完成。 It's a secure way. 这是一种安全的方法。 Once the check has succeeded, you can check if the field is present 一旦检查成功,您就可以检查该字段是否存在

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // handle the fields
    $packs = null;
    if (isset($_POST['pack']) {
        $packs = $_POST['pack'];
    }
}

Please notice that you are using pack without the [] to get input data from pack[] input fields. 请注意,您使用pack 而不 []获得来自输入数据pack[]输入字段。 The variable $pack will contain an array. 变量$pack将包含一个数组。 If you want to check the content, use var_dump($pack); 如果要检查内容,请使用var_dump($pack); . It should reveal all data from the available pack[] elements, either added dynamically or statically. 它应该显示可用pack[]元素中的所有数据,这些数据可以动态添加或静态添加。

Then, if you have collected all data, you have to loop through it. 然后,如果您已收集所有数据,则必须遍历所有数据。 It can be done with either for- or foreach-loop. 可以使用for或foreach循环来完成。

if ($packs != null) {
    foreach($packs as $pack) {
        // use the field $pack to do something
        echo 'amount of pack : ' . $pack;
    }
}

Do the same for other name fields which is also from array. 对同样来自数组的其他名称字段执行相同的操作。

From jQuery Doc: 从jQuery Doc:

Note: Using .clone() has the side-effect of producing elements with duplicate id attributes, which are supposed to be unique. 注意:使用.clone()具有产生具有重复id属性的元素的副作用,这些id属性应该是唯一的。 Where possible, it is recommended to avoid cloning elements with this attribute or using class attributes as identifiers instead. 建议尽可能避免克隆具有此属性的元素,或者改为使用类属性作为标识符。

You are cloning table row with #Row2 id. 您正在使用#Row2 id克隆表行。 To avoid it: 为了避免这种情况:

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

或者,您可以执行类似...

while (count-- > 0) first_row.clone().appendTo('#blacklistgrid').attr('id','Row' +              $('#blacklistgrid tr').length);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提交表单后,从帖子数组中动态附加的表行字段中获取值有什么问题? - What's the issue in getting the values from dynamically appended table row fields in post array after submitting the form? 提交时无法从 HTML 表单获取 jQuery 中的值 - Unable to get values in jQuery from HTML Form on submission 如何获得完整的表,其中从jQuery动态追加元素 - how to get complete table in which elements are appended dynamically from jquery 如何从jquery表单提交中获取所有切换值? - How to get all toggle values from form submission in jquery? 使用POST从手机jQuery应用PhoneGap跨域脚本检测POST成功提交HTML表单 - Detect successful html form submission using POST to cross-domain script from PhoneGap, mobile jQuery app 如何从表单获取值并防止提交? - How to get the values from a form and prevent submission? 使用 formData 提交表单后,php 中的 $_POST 数组为空 - $_POST array empty in php after submission of a form using a formData 如何验证动态添加的字段并防止使用jQuery提交表单? - How to validate dynamically added fields and prevent form submission using jQuery? HTML表单提交后从数组中提取数据(按键事件) - Extracting data from array after HTML form submission (keypress event) 在提交之前使用 javascript/jQuery 从表单中提取 post 参数 - extracting the post parameters from a form using javascript/jQuery before submission
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM