简体   繁体   English

表格每一行的订单按钮

[英]Order Button on each row of a table

I have a table of items and I would like to place an order button at the end of the table.我有一个物品表,我想在表的末尾放置一个订购按钮。 The button would then take user to "order.php".然后按钮会将用户带到“order.php”。 Where based on the "Picture ID" they would then order the selected item.然后他们会根据“图片 ID”订购所选项目。 I've had a look at similar questions online however haven't been able to get it working with what I currently have.我在网上看过类似的问题,但是我目前无法使用它。

if ($result->num_rows > 0)
{
    echo "<table>\n";
    while($row = $result->fetch_assoc())
    {
        echo "<tr>\n";
        echo "<td>". $row["Picture ID"]."</td>\n";
        echo "<td>". $row["name"]."</td>\n";
        echo "<td>". $row["doc"]."</td>\n";
        echo "<td>". $row["width"]."</td>\n";
        echo "<td>". $row["height"]."</td>\n";
        echo "<td>". $row["price"]."</td>\n";
        echo "<td>". $row["description"]."</td>\n";
        echo "<td> <form action='order.php' method='post'> <input type='button' name='id' value= ></form>"
        echo "</tr>\n";
    }
    echo "</table>\n";
}

You have to pass the picture id as hidden input text and use the "submit" type for the button:您必须将图片 ID 作为隐藏输入文本传递,并为按钮使用“提交”类型:

echo "<td> <form action='order.php' method='post'> <input type='hidden' name='id' value='" . $row["Picture ID"] . "'/> <input type='submit' name='submit' value='Buy'/></form>"

In order.php you can get the Picture ID from $_POST:在 order.php 中,您可以从 $_POST 获取图片 ID:

if (!empty($_POST['submit'])) { // submit button was pressed
  echo 'Picture ID: ' . $_POST['id'];
}

Make sure to escape the $_POST['id'] properly when using it in SQL queries.在 SQL 查询中使用 $_POST['id'] 时,请确保正确转义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM