簡體   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