douyue8364 2015-10-09 14:25
浏览 60
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • duanke6249 2015-10-09 14:34
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站