I can't find anything to help me with this issue...
I am trying to create a html order form for a user to select a product and save it as a php session variable. My preference is to use PHP as I am not overly familiar with ajax etc.
From what I have found, hidden variables can be used to capture the data which the user has selected, but I can't see how that works. Here is what I have so far (push add button to test):
example1.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<head>
<title>Order Form</title>
</head>
<body>
<?php
$p = array();
$p[0][0] = "Pants";
$p[0][1] = array("Small" => "$9.90", "Medium" => "$14.50", "Large" => "$19.90");
$p[0][2] = array("red", "blue", "green");
$p[1][0] = "Dress";
$p[1][1] = array("Small" => "$9.90", "Medium" => "$14.50", "Large" => "$19.90");
$p[1][2] = array("Orange", "White", "Blue", "Black");
$p[2][0] = "Shorts";
$p[2][1] = array("Small" => "$9.90", "Medium" => "$14.50", "Large" => "$19.90");
$p[2][2] = array("Yellow", "Blue");
echo '<form action="example2.php" method="post">';
// Print item name
for ($i = 0; $i < sizeof($p); $i++) {
echo "<table class='table'>";
echo "<tr><td colspan='4'>".$p[$i][0]."</td></tr>";
// Print colours
echo "<tr><td colspan='4'>";
for ($j = 0; $j < sizeof($p[$i][2]); $j++) {
if ($j == sizeof($p[$i][2]) - 1) {
echo ' & ';
} else if ($j != 0) {
echo ", ";
}
if ($j == 0) {
echo ucfirst($p[$i][2][$j]);
} else {
echo $p[$i][2][$j];
}
}
echo ".</td></tr>";
// Print prices
foreach ($p[$i][1] as $size => $price) {
echo "<tr class='size'>";
echo "<td width='400'>" . $size . "</td>";
echo "<td width='50' align='right'><b>" . $price . "</b></td>";
echo "<td><button name='customise' type='submit' value=" . $size . $p[$i][0] . " class='customise'>Customise</button><td>";
echo "<td><button name='add' type='submit' value=" . $size . $p[$i][0] . " class='add'>Add</button></td>";
echo "</tr>";
}
echo '</table>';
}
echo '<form>';
?>
</body>
</html>
example2.php
<?php
echo 'You ordered ' . $_POST["add"];
?>
The result is "You ordered LargePants"
I want to be able to save Large and Pants as 2 separate variables. How can I achieve this?
Right now you have one big form for all products, that's not going to work. Make one form for each product.
Do something like:
<?php
foreach ($products as $product) {
// html-table code here
?>
<form method="post" action="example2.php">
<input type="text" name="size" value="<?php echo $product["size"];?>" />
<input type="hidden" name="product" value="<?php echo $product["name"];?>" />
<input type="submit" value="Add" />
</form>
<?php
// more html-table code
}
Then you will get: $_POST["size"]
and $_POST["product"]
.
I made the size
field a text field but that will probably be a drop down or something. That's up to you.