将数组数据从表单传递到购物车
Passing array data from form to shopping cart
我找不到任何可以帮助我解决这个问题的东西...
我正在尝试为用户创建一个 html 订单表单以 select 产品并将其保存为 php 会话变量。我更喜欢使用 PHP,因为我不太熟悉 ajax 等
根据我的发现,隐藏变量可用于捕获用户 selected 的数据,但我看不出它是如何工作的。这是我到目前为止的内容(按添加按钮进行测试):
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" => ".90", "Medium" => ".50", "Large" => ".90");
$p[0][2] = array("red", "blue", "green");
$p[1][0] = "Dress";
$p[1][1] = array("Small" => ".90", "Medium" => ".50", "Large" => ".90");
$p[1][2] = array("Orange", "White", "Blue", "Black");
$p[2][0] = "Shorts";
$p[2][1] = array("Small" => ".90", "Medium" => ".50", "Large" => ".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"];
?>
结果是"You ordered LargePants"
我希望能够将 Large 和 Pants 保存为 2 个单独的变量。我怎样才能做到这一点?
现在您有一个适用于所有产品的大表格,这是行不通的。为每个产品制作一个表格。
做类似的事情:
<?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
}
那么你会得到:$_POST["size"]
和$_POST["product"]
。
我将 size
字段设为文本字段,但它可能是下拉列表或其他内容。由你决定。
我找不到任何可以帮助我解决这个问题的东西...
我正在尝试为用户创建一个 html 订单表单以 select 产品并将其保存为 php 会话变量。我更喜欢使用 PHP,因为我不太熟悉 ajax 等
根据我的发现,隐藏变量可用于捕获用户 selected 的数据,但我看不出它是如何工作的。这是我到目前为止的内容(按添加按钮进行测试):
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" => ".90", "Medium" => ".50", "Large" => ".90");
$p[0][2] = array("red", "blue", "green");
$p[1][0] = "Dress";
$p[1][1] = array("Small" => ".90", "Medium" => ".50", "Large" => ".90");
$p[1][2] = array("Orange", "White", "Blue", "Black");
$p[2][0] = "Shorts";
$p[2][1] = array("Small" => ".90", "Medium" => ".50", "Large" => ".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"];
?>
结果是"You ordered LargePants"
我希望能够将 Large 和 Pants 保存为 2 个单独的变量。我怎样才能做到这一点?
现在您有一个适用于所有产品的大表格,这是行不通的。为每个产品制作一个表格。
做类似的事情:
<?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
}
那么你会得到:$_POST["size"]
和$_POST["product"]
。
我将 size
字段设为文本字段,但它可能是下拉列表或其他内容。由你决定。