PHP表单提交$ _POST到购物车

时间:2015-01-14 02:37:04

标签: php html forms oop cart

首先,我要显示产品列表。我希望将这些产品输入到表格中并生成独特的按钮(我想我正确地做了这个部分)。但是,每当我提交例如产品2时,它会直接转到其他地方并接受值6。

 function displayData($result){
    print "<table border = 1 >"; 
print '<form id="form1" name="form1" method="post" action="cart.php">';
        while ($row = $result->fetch_assoc()){ 
      $image = $row["Image"];
            print "  <tr>"; 
      print "    <td valign='top'><ul><li>ID: " . $row["id"] . "</li><br />"; 
      ?>
      <input type="hidden" name="<?php echo $row["id"]; ?>" id="<?php echo $row["id"]; ?>" value="<?php echo $row["id"]; ?>"/>
      <input type="submit" name="button<?php echo $row["id"]; ?>" id="button<?php echo $row["id"]; ?>" value="Add item <?php echo $row["id"]; ?>"/> <br><?php
      print "    <li>Name: " . $row["Name"] . "</li><br />"; 
      print "    <li>Tag: " . $row["Tag"] . "</li></td></ul>"; 
      print "    <td valign='top'>" . $row["Description"] . "</td>"; 
      print "    <td> <img width=150px height=150px src='../../assets/images/$image'></td>"; 
            print "  </tr>"; 
        } 
   print " </form>";
    print "</table>"; 
   }//end of display function

这似乎可以正确生成按钮,例如&#34; button1&#34;和&#34;按钮2&#34;,现在在我的购物车上我尝试将提交内容分配给正确的产品ID,此时它只是输入值 6

if (isset($_POST['button1'])) {
    $pid = 1;
} else if (isset($_POST['button2'])) {
    $pid = 2;
} else if (isset($_POST['button3'])) {
    $pid = 3;
} else if (isset($_POST['button4'])) {
    $pid = 4;
} else if (isset($_POST['button5'])) {
    $pid = 5;
} else  {
    $pid = 6;
    $wasFound = false;
    $i = 0;
    //If the cart session variable is not set or cart array is empty
    if (!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){
        //Run if the cart is empty or not set
        $_SESSION["cart_array"] = array(1 =>array("item_id" => $pid, "quantity" => 1));
    } else {
            //Run if the cart has at least one item in it
        foreach($_SESSION["cart_array"] as $each_item){
            $i++;
            while(list($key, $value) = each($each_item)) {
                if ($key == "item_id" && $value==$pid) {
                    //That item is in the cart already so just adjust the quantity using array splice()
                    array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity']+1)));
                    $wasFound = true;
                } //Close if condition
            } //Close while loop
        } //Close foreach loop
    if ($wasFound == false) {
        array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" =>1));
    }//Close if statement 
}//Close if statement 
}

这是HTML表单的输出 - 按要求。

<table border = 1 ><form id="form1" name="form1" method="post" action="cart.php">  <tr>    <td valign='top'><ul><li>ID: 1</li><br />          <input type="hidden" name="1" id="1" value="1"/>
      <input type="submit" name="button1" id="button1" value="Add item 1"/> <br>    <li>Name: Hamburger</li><br />    <li>Tag: Burger</li></td></ul>    <td valign='top'>A minced beef patty in between some bread</td>    <td> <img width=150px height=150px src='../../assets/images/burger.jpg'></td>  </tr>  <tr>    <td valign='top'><ul><li>ID: 2</li><br />          <input type="hidden" name="2" id="2" value="2"/>
      <input type="submit" name="button2" id="button2" value="Add item 2"/> <br>    <li>Name: Cheeseburger</li><br />    <li>Tag: Burger</li></td></ul>    <td valign='top'>A minced beef patty with cheese in between some bread</td>    <td> <img width=150px height=150px src='../../assets/images/cheeseburger.jpg'></td>  </tr>  <tr>    <td valign='top'><ul><li>ID: 3</li><br />          <input type="hidden" name="3" id="3" value="3"/>
      <input type="submit" name="button3" id="button3" value="Add item 3"/> <br>    <li>Name: Chicken Burger</li><br />    <li>Tag: Burger</li></td></ul>    <td valign='top'>A butter flied chicken breast in between some bread</td>    <td> <img width=150px height=150px src='../../assets/images/chickenburger.jpg'></td>  </tr>  <tr>    <td valign='top'><ul><li>ID: 4</li><br />          <input type="hidden" name="4" id="4" value="4"/>
      <input type="submit" name="button4" id="button4" value="Add item 4"/> <br>    <li>Name: Spaghetti Bolognese</li><br />    <li>Tag: Pasta</li></td></ul>    <td valign='top'>Traditional Italian pasta dish</td>    <td> <img width=150px height=150px src='../../assets/images/bolognese.jpg'></td>  </tr>  <tr>    <td valign='top'><ul><li>ID: 5</li><br />          <input type="hidden" name="5" id="5" value="5"/>
      <input type="submit" name="button5" id="button5" value="Add item 5"/> <br>    <li>Name: Sirloin</li><br />    <li>Tag: steak</li></td></ul>    <td valign='top'>God tier beef</td>    <td> <img width=150px height=150px src='../../assets/images/steak.jpg'></td>  </tr> </form></table>

提前致谢

1 个答案:

答案 0 :(得分:0)

不是尝试使用繁琐的if语句选择项目,而是更好地实现for循环。这是让它运作的代码。

$result = $mysqli_conn->query("SELECT DISTINCT id FROM Food");
//print " Query returned ". $result->num_rows . " rows ";; 
$len = $result->num_rows;
for($k=1; $k<=$len; $k++)
{
    if (isset($_POST['button'.$k]))
    {
        $pid = $k;
    }
}