How does one get the value from a text input element on a dynamic form built from an included file to a script in the calling page? I have tried the $_POST method and although it does grab the original value from the text input field, if I change that value like you might want to do on a shopping cart for instance, it does not get that value.
Below is part of my code on the calling page that is supposed to save the quantity change to the session var 'cart':
if($_POST['save'])
{
echo "<font color = \"#FFFFFF\">the cart has been changed and saved.</font><br>";
var_dump($_SESSION['cart']);
echo "<br>";
foreach ($_SESSION['cart'] as $itemSku => $qty)
{
echo "<font color = \"#FFFFFF\" size = 1>The quantity of $itemSku is $qty</font><br>";
echo "<font color = \"#FFFFFF\" size = 1>form value is " . $_POST['quantity'] . "</font><br>";
if($_POST['quantity']=="0")
unset($_SESSION['cart'][$itemSku]);
else
$_SESSION['cart'][$itemSku] = $_POST['quantity'];
}
$total_price = calculate_price($_SESSION['cart']);
$items = calculate_items($_SESSION['cart']);
}
Then below is the form code from the include file:
function display_cart($cart, $change = true, $images = 0)
{
// display items in shopping cart
// optionally allow changes (true or false)
// optionally include images (1 - yes, 0 - no)
//global $items;
//global $total_price;
echo "<table border = 0 width = 100% cellspacing = 0>
<form action = cart.php method = post>
<tr class=\"tbleHdr\"><th align=left colspan = ". (1+$images) .">Item</th>
<th>Price</th><th>Quantity</th>
<th>Total</th></tr>";
//display each item as a table row
foreach ($_SESSION['cart'] as $itemSku => $qty)
{
$items = get_item_details($itemSku);
echo "<tr>";
if($images ==true)
{
echo "<td align = left>";
if (file_exists("images/$itemSku.jpg"))
{
$size = GetImageSize("images/".$itemSku.".jpg");
if($size[0]>0 && $size[1]>0)
{
echo "<img src=\"images/".$itemSku.".jpg\" border=0 ";
echo "width = ". $size[0]/3 ." height = " .$size[1]/3 . ">";
}
}
else
echo " ";
echo "</td>";
}
echo "<td class=\"record\" align = left>";
echo "<a href = \"order_a.php?itemSku=".$itemSku."\">".$items["itemName"];
echo "</td><td class=\"record\" align = center>$".number_format($items["itemPrice"], 2);
echo "</td><td class=\"record\" align = center>";
// if we allow changes, quantities are in text boxes
if ($change == true)
echo "<input type = text class=\"record\" name = \"quantity\" value = $qty size = 3>";
else
echo $qty;
echo "</td><td class=\"record\" align = center>$".number_format($items["itemPrice"]*$qty,2)."</td></tr>\n";
}
// display total row
echo "<tr class=\"tbleHdr\">
<th class=\"tbleHdr\" colspan = ". (2+$images) ."> </th>
<th class=\"tbleHdr\" align = center>" . $_SESSION['items'] . "
</th>
<th class=\"tbleHdr\" align = center>
\$".number_format($_SESSION['total_price'], 2).
"</th>
</tr>";
// display save change button
if($change == true)
{
$target = "index.php";
echo "<tr>
<td align = center>" ?>
<?php display_button("NULL", $target, "continue-shopping", "Continue Shopping");
<?php echo "</td>
<td align = center>" ?>
<?php $path = $_SERVER['PHP_SELF'];
//echo "$path";
$path = str_replace("cart.php", "", $path);
display_button("NULL", "https://".$_SERVER['SERVER_NAME'].$path."checkout.php", "go-to-checkout", "Go To Checkout"); ?>
<?php echo "
<td align = center>
<input type = hidden name = save value = true>
<input type = image src = \"assets/images/save-changes.gif\"
border = 0 alt = \"Save Changes\">
</td>
<td> </td>
</tr>";
}
echo "</form></table>";
}
Please help if you can. I really need to solve this for my sanity sake.
Cheers
D