通过Ajax在一个表单中输出两个变量

问题描述 投票:0回答:1

在这里,我加载一个表格,其中包含输入数量的选项。

<?php
while ($row=mysql_fetch_array($select)) 
{
?>

<td><?php echo $row['Size'];?></td>    
<td><input type="text" name="[<?php echo $row['Id'];?>]" id="[<?php echo $row['Id'];?>]"></td>

<?php
}
?>

提交表单时,我想通过Ajax将所有输入+ ID发布到下一页。

$(document).ready(function() {
    var table = $('#Productslist').DataTable();

    $('button').click( function() {
        var data = table.$('input').serialize();
        $.ajax({
        data: data.substr( 0, 120 ),
        url: 'confirmorder2.php',
        method: 'POST', // or GET
        });
});
    } );

在这里,我试着让每一行都在ID=$nameinput=$value。如果输入为空,则不会回显。

<?php
foreach($_POST as $name => $value)
{
if (!empty($value)) { 
  ?>
  <tr>
  <td><?php echo $name;?></td>
  <td><?php echo $value;?></td>
  </tr>
  <?php
  }
}
?>

我在下一页上回应的唯一一件事就是我的隐藏输入中的令牌名称和令牌。

javascript php jquery ajax forms
1个回答
0
投票

serialize()函数用于序列化表单中的所有输入值,而不是您可以使用以下代码。

    $(document).ready(function() {
    var table = $('#Productslist').DataTable();

    $('button').click( function() {


$("#Productslist tbody tr").each(function(index, el) {
    var input = $(this).children(':nth-child(1)').find('input');
    var data = {};
    data.name = input.value;
    data.id = input.attr('id');
 $.ajax({
        data: data.substr( 0, 120 ),
        url: 'confirmorder2.php',
        method: 'POST',
        success:function(response){
console.log(response);
        } 
        });
});
});
});
© www.soinside.com 2019 - 2024. All rights reserved.