简体   繁体   中英

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

I have a dynamic table with 4 textboxes qty, price, discount and subtotal in each row. How to get an array with each values entered in each textbox in each row in $_POST? Like this for example:

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

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

This is my code:

<?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++;
}
?>  

Thank you

Not 100% sure I grasped the true nature of the question and the following has not been tested but will, I hope, be of use - there is no sanity / validity checking of supplied POST data here though

$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 ) );

Make each HTML name an array with an index.

<?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++;
}
?>  

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