For eg I have this code on the main page.
<?php
session_start();
$_SESSION['order']=array();
?>
<form name="orderform" method="post" action="e.php">
Product Catalog
<table border="1">
<tr>
<td>Product</td>
<td>Price</td>
<td>Quantity</td>
</tr>
<?
for($i=0;$i<6;$i++){
echo '<tr>';
echo '<td><input type=hidden name="product" value="'.$i.'"> Product '.$i.'</td>';
$price=rand(1,10);
echo '<td><input type=hidden name="price" value="'.$price.'">$'.$price.'</td>';
echo '<td><input type=text name="quantity"></td>';
echo '<tr>';
}
?>
</table>
<br>
<input type="submit" name="submit" value="submit">
</form>
I have a multidimensional Session array, $_SESSION['order'] and I'm trying to save this order form of 6 product items, along with its price and quantity, so it can be retrieved on the following page after POST method is actioned.
ie on e.php file
<?php
session_start();
$_SESSION['order'][] = array('product'=>$_POST['product'],
'price'=>$_POST['price'],
'quantity'=>$_POST['quantity']);
var_dump($_SESSION['order']);
if(count($_SESSION['order'])>0){
foreach($_SESSION['order'] as $order){
echo "<p>Product = ".$order['product']."</p>";
echo "<p>Price = ".$order['price']."</p>";
echo "<p>Quantity = ".$quantity['quantity']."</p>";
}
}
?>
But the result I'm getting on e.php is I only get the last item of the order page, but not the other previous five. Did I do something wrong here? What are your thoughts?
You're _POST
ing multiple fields with the same name
property, so the $_POST
variable will contain only the final unique names that you're posting up. You can either create unique names for each input, or you can post up the fields as an array. Unique names would look like this:
<?
for($i=0;$i<6;$i++){
echo '<tr>';
echo '<td><input type=hidden name="product'.$i.'" value="'.$i.'"> Product '.$i.'</td>';
$price=rand(1,10);
echo '<td><input type=hidden name="price'.$i.'" value="'.$price.'">$'.$price.'</td>';
echo '<td><input type=text name="quantity'.$i.'"></td>';
echo '<tr>';
}
?>
And then you'd need to loop through the post array and add it to session array:
<?php
$i=0;
while(isset($_POST['product'.$i])){
$_SESSION['order'][] = array('product'=>$_POST['product'],
'price'=>$_POST['price'],
'quantity'=>$_POST['quantity']);
$i++;
}
?>
You can also send it up as an array:
<?
for($i=0;$i<6;$i++){
echo '<tr>';
echo '<td><input type=hidden name="product[$i]" value="'.$i.'"> Product '.$i.'</td>';
$price=rand(1,10);
echo '<td><input type=hidden name="price[$i]" value="'.$price.'">$'.$price.'</td>';
echo '<td><input type=text name="quantity[$i]"></td>';
echo '<tr>';
}
?>
And get it out like this:
<?php
foreach($_POST['product'] as $key => $value){
$_SESSION['order'][] = array('product'=>$_POST['product'][$key],
'price'=>$_POST['price'][$key],
'quantity'=>$_POST['quantity'][$key]);
}
?>
The problem is you have multiple form fields with the same name. You'd need something like this:
for($i=0; $i<6; $i++){
$price = rand(1,10);
printf('<tr>'.
'<td><input type=hidden name="product[%1$d]" value="%1$d" />Product %1$d</td>'.
'<td><input type=hidden name="price[%1$d]" value="%2$f" />$ %2$f</td>'.
'<td><input type=text name="quantity[%1$d]" /></td>'.
'</tr>',
$i, $price);
}
and
for ($i = 0; $i < count($_POST['product']; $i++) {
$_SESSION['order'][] = array('product'=>$_POST['product'][$i],
'price'=>$_POST['price'][$i],
'quantity'=>$_POST['quantity'][$i]);
(For production you should also check the POST variables if they are really present and arrays in correct size.)