在While循环中提交按钮

时间:2017-04-08 14:25:30

标签: php while-loop html-form htmlbutton

我的产品以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);
?>            

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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