Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop on multiple post form values using PHP

I have a form where I am editing my orders.On my edit order page, I am breaking down my orders.For example, Order details (Customer name,address etc) and then order items like this:

Order Details:

     <form name="edit_order" id="edit_order" method="post">
      <? 
     if ($order_details) {
                //    print_array($order_details);
                    foreach ($order_details as $key => $link) {
                    ?>
                    <div width="200">
                        <div><b>Product: </b> <br><?=$link->product_name?> - <?=$link->product_id?></div>  
                        <div><strong>Cost:</strong><br>&pound;<?=$link->unit_cost?></div>
                        <div><strong>Pack Qty:</strong><br><input name="quantity" id="quantity" value="<?=$link->quantity?>" style="width:50px;"   /> </div> 
                        <div><strong>Pack Cost:</strong> <br><input name="pack_cost" id="pack_cost" value="<?=$link->pack_cost?>" style="width:50px;" />   </div>
                           <div><strong>Pallet Qty:</strong><br><input name="pallet_quantity" id="pallet_quantity" value="<?=$link->pallet_quantity?>" style="width:50px;" />  </div>
                           <div><strong>Pallet Cost:</strong><br><input name="pallet_quantity" id="pallet_quantity" value="<?=$link->pallet_unit?>" style="width:50px;" />  </div>

                    </div>
                    <?
                    }
                }
           ?>

MY Question is :

How can I retrieve these values for multiple fields(input boxes) on next page?

For example,if I edit "pallet_quantity" and "pack_cost" of 10 products on edit order page then how can i retrieve them on next page using the loop to save them in the database?.

When I try to display values on web page like this : print($_POST['pack_cost']) ,it gives me single value.How can I loop through the POST array to display and save my edited values into the database?. Help me plz.

like image 316
Bilal Khalid Avatar asked Dec 18 '25 14:12

Bilal Khalid


1 Answers

You can make array for input values. Look the below code ...

<input name="pack_cost[]" id="pack_cost1" value="<?=$link->pack_cost?>" style="width:50px;" />
<input name="pack_cost[]" id="pack_cost2" value="<?=$link->pack_cost?>" style="width:50px;" />
<input name="pack_cost[]" id="pack_cost3" value="<?=$link->pack_cost?>" style="width:50px;" />

just remember that the id of these input boxes should be different.

So, when you make echo $_POST['pack_cost'] on next page, you will get an array named pack_cost containing key and value.

EDITED

WHOLE CODE :-

<form name="edit_order" id="edit_order" method="post">
<? 
if ($order_details) {
     //    print_array($order_details);
     foreach ($order_details as $key => $link) {
?>
    <div width="200">
        <div><b>Product: </b> <br><?=$link->product_name?> - <?=$link->product_id?></div>  
        <div><strong>Cost:</strong><br>&pound;<?=$link->unit_cost?></div>
        <div><strong>Pack Qty:</strong><br><input name="quantity[]" id="quantity<?php echo $key; ?>" value="<?=$link->quantity?>" style="width:50px;"   /> </div> 
        <div><strong>Pack Cost:</strong> <br><input name="pack_cost[]" id="pack_cost<?php echo $key; ?>" value="<?=$link->pack_cost?>" style="width:50px;" />   </div>
        <div><strong>Pallet Qty:</strong><br><input name="pallet_quantity[]" id="pallet_quantity<?php echo $key; ?>" value="<?=$link->pallet_quantity?>" style="width:50px;" />  </div>
        <div><strong>Pallet Cost:</strong><br><input name="pallet_quantity[]" id="pallet_quantity<?php echo $key; ?>" value="<?=$link->pallet_unit?>" style="width:50px;" />  </div>
    </div>
 <?
    }
 }
 ?>
</form>

This is what, which will outputs your need.

Below is the code for fetching your return values

if($_POST)
{
    $field_array = array();
    for( $i=0 ; $i < count($_POST['pack_cost']) ; $i++ )
    {
        $field_array[$i]['quantity']        = $_POST['quantity'][$i];
        $field_array[$i]['pack_cost']       = $_POST['pack_cost'][$i];
        $field_array[$i]['pallet_quantity'] = $_POST['pallet_quantity'][$i];
        $field_array[$i]['pallet_cost']     = $_POST['pallet_cost'][$i];

        // if you require then the query for your database
    }
}
like image 79
anuj arora Avatar answered Dec 20 '25 10:12

anuj arora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!