2017-09-06 61 views
2

我的序列化表单有问题。我需要传递多行来创建发票,但它只传递第一行。在PHP中通过序列化jQuery传递多个参数

这是一种形式:

<form action="" id="generate_invoice" method="POST"> 
    <td><input type="number" class="form-control n_invoice" name="n_invoice[]"></td> 
    <td><input type="date" class="form-control data" name="data[]"></td> 
    <td><input type="text" class="form-control description" name="description[]"></td> 
    <td><input type="number" class="form-control price" name="price[]" step=any></td> 
    <td><input type="number" class="form-control vat" name="vat[]"> 
</form> 

这是它增加了新行的功能:

function addrow_invoice() { 
    var i = $('#invoiceTable tr').length; 
    var tr = '<tr>'+ 
    '<td><input type="checkbox" class="case"/></td>'+ 
    '<td></td>'+ 
    '<td></td>'+ 
    '<td><input type="text" class="form-control description" name="description[]"></td>'+ 
    '<td><input type="number" class="form-control price" name="price[]"></td>'+ 
    '<td><input type="number" class="form-control vat" name="vat[]"></td>'+ 
    '</tr>'; 
    $('table#invoiceTable').append(tr); 
    i++; 
}; 

这是一个测试:

for($i = 0; $i<count($_POST['description']); $i++) 
{ 
    echo "{$_POST['description'][$i]}"; 
    echo "<br>"; 
} 

回答

0

的问题是因为你的HTML无效。 form需要包装table元素,不在里面。由于HTML无效,因此form的位置会移动,因此它不会包装您的input元素,因此没有任何内容被序列化。

你的HTML需要改变这样:

<form action="" id="generate_invoice" method="POST"> 
 
    <table id="invoiceTable"> 
 
    <tr> 
 
     <td><input type="number" class="form-control n_invoice" name="n_invoice[]"></td> 
 
     <td><input type="date" class="form-control data" name="data[]"></td> 
 
     <td><input type="text" class="form-control description" name="description[]"></td> 
 
     <td><input type="number" class="form-control price" name="price[]" step=any></td> 
 
     <td><input type="number" class="form-control vat" name="vat[]"> 
 
    </tr> 
 
    <!-- additional rows appended here... --> 
 
    </table> 
 
</form>

+0

真正的问题是,当我经过他计算一个参数,但如果打印没有显示什么 –