Jump to content

strange thing about $_POST array when it's passed another page


Duke555

Recommended Posts

i have noticed a strage thing about a $_POST array and i was wondering if someone could help me understand how $_POST works better.

in the form below with use 'post' in order access default values (stored by $temp_number) product quantities one would have to use:

 

$_POST
['cart']
["$name"]

 

and it makes sense since this form has a 'name="cart"'

 

However, once a user cick on SUBMIT and he is taken to my_cart.php

 

than to access quantities of goods from the user's input one has to use:

 

$_SESSION['cart'][$name]['qty']=(int)$_POST
["$name"];

 

which means one cannot access quantities of products input by the user

via $_POST['cart']["$name"]

 

why is this like that?

should not the $_POST array be the same when it is passed on to another page?

 

than you :)

 

<form action="my_cart.php" name="cart" method="post">

.....
.....
	<td align="left"><input type="text" size="3" name="<?echo $name;?>" value='<?$temp_number = 0; echo $_POST['cart']["$name"]=$temp_number;?>' /></td>
		<td align="right"><? echo number_format($grand_total, 2);?></td>
	</tr>
	<? 
	$_SESSION['cart'][$name]['qty']=(int)$_POST['cart']["$name"];
}

...[/code]

If you echo the values as INPUT values...as it seems you do....then those values should be available via POST on my_cart.php when the form is posted there.

 

On the page with the form and the default values, if you do a View Source, what does the HTML for the form look like? Is there any info missing? Wrong input names?

thank you for your reply.

below is the full code.

i have not found any anomalies with variable names and etc.

 

 

<form action="my_cart.php" name="cart" method="post">
<?
//traversing outer array in this loop
//'name' is the 'key' and 'description' is the 'value' of this outer array
while (list($name, $description) = each($product))
{
	//calculating grand total of an item's 'price' and 'shipping' which are stored in the inner
	//array and are pointed by the keys 'price' and 'shipping' respectively
	$grand_total = calc_total($description["Price"], $description["Shipping"]);
	$shipping = $description["Shipping"] * $description["Price"];
?>
<tr>
	<td align="left"><? echo $name;?></td>
	<td align="left"><? echo number_format($description["Price"], 2);?></td>
	<td align="left"><? echo number_format($shipping, 2); ?></td>
	<td align="left"><input type="text" size="3" name='<?echo $name;?>' value='<?$temp_number = 0; echo $_POST['cart'][$name]=$temp_number;?>' /></td>
		<td align="right"><? echo number_format($grand_total, 2);?></td>
	</tr>
	<? 
	$_SESSION['cart'][$name]['qty']=(int)$_POST['cart'][$name];
	echo "<br>\$name ".$name."<br>";
	echo "<br>'\$name' ".$name."<br>";		
}
echo "<br>printing _POST<br>";
print_r($_POST);
echo "<br>printing _SESSION<br>";
print_r($_SESSION);	
echo "<br>";
	// Print the footer, close the table, and the form:
	?><tr>
		<td colspan="4" align="right"><b>Total:</b></td>
		<td align="center">TEST</td>
	</tr>	
	<tr>
		<td colspan="6" align="center">TEST</td>
	</tr>
	</table><div align="center"><input type=submit value="SUBMIT"></div>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.