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.

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

报告相同问题?

悬赏问题

  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00
  • ¥15 odbc代码新增sqlserver数据源
  • ¥15 求一个USB指纹识别的驱动
  • ¥15 wps中工作表与工作表之间怎么汇总信息?
  • ¥50 请教Windows server 2012 R2的DCOM配置问题