While 循环中的提交按钮

Submit Button in While Loop

我有以 while 循环形式打印到屏幕上的产品以及与之配套的 'add_to_cart' 按钮。

我希望能够按下 "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);
?>            

您好,您似乎没有添加任何内容来通过 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);

但是,如果您希望它每次都添加或迭代产品数量,以便变量存储许多产品。您将必须使用会话变量

 <?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);
?>