【问题标题】:Passing array data from form to shopping cart将数组数据从表单传递到购物车
【发布时间】:2015-10-09 14:25:20
【问题描述】:

我找不到任何可以帮助我解决这个问题的东西...

我正在尝试创建一个 html 订单表单,供用户选择产品并将其保存为 php 会话变量。我的偏好是使用 PHP,因为我对 ajax 等不太熟悉。

据我发现,隐藏变量可用于捕获用户选择的数据,但我看不出它是如何工作的。这是我目前所拥有的(按下添加按钮进行测试):

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"];
?>

结果是“您订购了大裤衩”

我希望能够将 Large 和 Pants 保存为 2 个单独的变量。我怎样才能做到这一点?

【问题讨论】:

  • 您的购物车在哪里?我也期待看到一些处理会话的代码。
  • 我还没有创建购物车。我只是想先弄清楚如何从表格中取出数据。再次......在会话中,一旦我让 POST 工作,我会做所有这些。

标签: php html shopping-cart


【解决方案1】:

现在您有一个适用于所有产品的大表单,但这是行不通的。为每个产品制作一个表格。

执行以下操作:

<?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 字段设为文本字段,但这可能是一个下拉菜单或其他内容。这取决于你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2022-08-23
    • 2019-01-13
    • 1970-01-01
    相关资源
    最近更新 更多