繁体   English   中英

如何保存在动态表的每一行中输入的文本框值

[英]How to save textbox values entered in each row of a dynamic table

我有一个动态表,每行有4个文本框,分别为数量,价格,折扣和小计。 如何获得一个数组,该数组具有在$ _POST的每一行的每个文本框中输入的每个值? 例如:

[0]=> {
    ["Price"]=>10
    ["Qty"]=>5
    ["Discount"]=>1
    ["Subtotal"]=>49
}

[1]=> {
    ["Price"]=>5
    ["Qty"]=>10
    ["Discount"]=>2
    ["Subtotal"]=>48
}

这是我的代码:

<?php
while($iArticles < count($listeArticlePourUnDossier))
{
?>  
    <tr>
        <td><?php echo ($listeArticle[$iArticles]['name']); ?></td>
        <td><input type="text" name="Price[]" id="Price"/></td>
        <td><input type="text" name="Qty[]" id="Qty" /></td>
        <td><input type="text" name="Discount[]" id="Discount" /></td>
        <td><input type="text" name="Subtotal[]" id="Subtotal" /></td>
    </tr>
<?php       
$iArticles++;
}
?>  

谢谢

不能100%确定我已掌握问题的真实性质,并且未测试以下内容,但希望可以使用-尽管此处没有对提供的POST数据进行完整性/有效性检查

$prices=$_POST['Price'];
$qtys=$_POST['Qty'];
$discounts=$_POST['Discount'];
$subs=$_POST['Subtotal'];

$data=[];

foreach( $prices as $index => $price ){
    $data[]=[
        'price'     =>  $price,
        'qty'       =>  $qtys[ $index ],
        'discount'  =>  $discounts[ $index ],
        'subtotal'  =>  $subs[ $index ]
    ];
}

printf( '<pre>%s</pre>', print_r( $data, true ) );

使每个HTML名称成为带有索引的数组。

<?php
$x=0;
while($iArticles < count($listeArticlePourUnDossier))
{
  $x++;
?>  
    <tr>
        <td><?php echo ($listeArticle[$iArticles]['name']); ?></td>
        <td>
            <input type="text" name="Price[<?php echo $x; ?>]" id="Price_<?php echo $x; ?>"/>
            <input type="text" name="Qty[<?php echo $x; ?>]" id="Qty_<?php echo $x; ?>" />
            <input type="text" name="Discount[<?php echo $x; ?>]" id="Discount_<?php echo $x; ?>" />
            <input type="text" name="Subtotal[<?php echo $x; ?>]" id="Subtotal_<?php echo $x; ?>" />
        </td>
    </tr>
<?php       
$iArticles++;
}
?>  

暂无
暂无

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

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