php While循环中的Submit按钮

2ic8powd  于 2022-10-30  发布在  PHP
关注(0)|答案(2)|浏览(182)

我有一些产品以while循环的形式打印到屏幕上,并带有“add_to_cart”按钮。
我希望能够按下“添加到购物车”按钮,并将其上方的选定产品添加到“$selectedProduct”变量中。但当我按下该按钮时,它没有发生任何变化,变量保持空白。

<?php 

  include('connect.php'); 
  require("session.php"); 

        //create the sql statement 
        $sql_all=
             "SELECT * FROM product 
              ORDER BY product_ID DESC";
        $result_all = mysqli_query($con,$sql_all);

        //PRINT OUT ALL THE PRODUCT
        while ($row_all = mysqli_fetch_assoc($result_all)) 
                {
                    echo '<form method="post">';
                        echo '<u>'.$row_all["name"].'</u>';

                        echo '<br>';

                        echo '<button name="add_to_cart" type="submit">Add to Cart</button>';

                        echo '<hr>';
                    echo '</form>';

                }

            if(isset($_POST["add_to_cart"]))
            {

                //CREATE A VARIABLE THAT HOLDS THE SELECTED PRODUCTED TO BE ADDED TO CART
                    $selectedProduct = $row_all["name"];

                echo 'Selected Product  = '.$selectedProduct;

            }

    mysqli_close($con);
?>
mznpcxlj

mznpcxlj1#

你好,你似乎还没有添加任何东西来通过post方法。找到下面的代码,我已经告诉你如何做正确的方式。这将添加所选的产品到该特定的变量每一次。

while ($row_all = mysqli_fetch_assoc($result_all)) 
                {
                    echo '<form method="post">';
                        echo '<u>'.$row_all["name"].'</u>';

                        echo '<br>';

                        echo '<button name="add_to_cart" value='.$row_all['name'].' type="submit">Add to Cart</button>';

                        echo '<hr>';
                    echo '</form>';

                }

            if(isset($_POST["add_to_cart"]))
            {

                //CREATE A VARIABLE THAT HOLDS THE SELECTED PRODUCTED TO BE ADDED TO CART
                    $selectedProduct = $_POST["add_to_cart"];

                echo 'Selected Product  = '.$selectedProduct;

            }

    mysqli_close($conn);

但是,如果你希望它每次添加或迭代产品的数量,以便变量存储许多产品,你将不得不使用会话变量

tzcvj98z

tzcvj98z2#

<?php 

  include('connect.php'); 
  require("session.php"); 

        //create the sql statement 
        $sql_all=
             "SELECT * FROM product 
              ORDER BY product_ID DESC";
        $result_all = mysqli_query($con,$sql_all);
while ($row_all = mysqli_fetch_assoc($result_all)) 
                {
                    echo '<form method="post">';
                        echo '<u>'.$row_all["name"].'</u>';

                        echo '<br>';

                        echo '<button name="add_to_cart" value='.$row_all['name'].' type="submit">Add to Cart</button>';

                        echo '<hr>';
                    echo '</form>';

                }

            if(isset($_POST["add_to_cart"]))
            {

                //CREATE A VARIABLE THAT HOLDS THE SELECTED PRODUCTED TO BE ADDED TO CART
                    $selectedProduct = $_POST["add_to_cart"];

                echo 'Selected Product  = '.$selectedProduct;

            }

        mysqli_close($conn);
?>

相关问题