3

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.

1
  • Bilal Khalid - please follow me on twitter.com/Le3jeb Commented Jan 14, 2013 at 7:20

3 Answers 3

4

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
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

hey anuj i tried your idea. The array is giving general like this Array ( [0] => 44990 [1] => 44364 [2] => 436467 [3] => 477487 [4] => 893747 [5] => 347467 ) But can i change array values with my own values like Array( [123] =>45678 [145] =>556789) like this..........
Actually why i want like this is i need to insert them back in the database. For that i need to identify them using unique id.
@anujarora.Could you edit your answer to show how multiple values can be retrieved based on the index values on the next page?
I wish it would be like so, but $_POST fetches the values of input field by --name-- not by --id--. But i think using array in form is only used for multiple fields with same name and input type. Not for all input field types.
@anujarora.Hi anuj.I want the output.I wanted to know how would you retrieve array values for multiple products.For example,if i edit 10 products on the above page then how would i retrieve the input field values for each product??Any idea.THATs ALL i want.Thanks
|
1

As far as I undertand, you want to do this:

foreach ($_POST as $tag => $value) {
// Do whatever you want with the params
}

Comments

0

USE SESSION LEVEL STORAGE

(i.e) $_SESSION[ID][field1], $_SESSION[ID][field2], $_SESSION[ID][field3]

While moving from one page to another page for getting other fields.. you store the datas from previous page in SESSION with one session ID.. and in next upcoming pages do the same thing.. but use the same session ID for one entire Form... after that display in next or last page... or if you need store the data into DB(at end of the process) from session variable and unset/remove all those session datas including id if you dont want...

For Codeigniter, you use session library for these concept... please refer to assign and retrieve value to session variable,,

CodeIgniter Session

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.