繁体   English   中英

将数组数据从表单传递到购物车

[英]Passing array data from form to shopping cart

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

我正在尝试为用户创建一个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"];
?>

结果是“您订购了LargePants”

我希望能够将大号和裤子另存为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字段设置为文本字段,但这可能是下拉列表或其他内容。 随你(由你决定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM