所以我正在尝试制作一个购物车。我需要做的是将用户想要购买的产品 ID 和数量发送到另一个文件 cart.php
.
这是我的代码的一部分:
for($i=0;$i<$numOfRows;$i++){
$prodID=mysql_result($result, $i, "ProductID");
$prodTitle=mysql_result($result, $i, "Title");
$prodAuthor=mysql_result($result, $i, "Author1");
$prodPrice=mysql_result($result, $i, "Price");
Print"<h4>ID: $prodID \n </h4>";
Print"<h4>Title: $prodTitle \n </h4>";
Print"<h4>Author: $prodAuthor \n </h4>";
Print"<h4>Price: $ $prodPrice \n </h4>";
Print" <form method ='POST'>";
Print"<p><label> Quantity";
Print"<input name='quantity' type='text' pattern='[1-9]{1,50}'/>";
Print"</label>";
Print"<input type='button' value='Add To Cart' onClick='cart.php?pid=$prodID'/>";
Print"</p></form>";
}
如果我去掉表格并只有 <a href='cart.php?pid=$prodID'>Add To Cart</a>
它发送了 id,但我还需要与之配套的数量。
每当我点击添加到购物车时,它不会将我发送到我的cart.php
.
最佳答案
如果您想使用 POST 方法,请添加带有 pid 信息的隐藏输入。请参阅示例:
print" <form method ='POST' action ='cart.php'>";
print"<p><label> Quantity";
print"<input name='quantity' type='text' pattern='[1-9]{1,50}'/>";
print"</label>";
print"<input type='hidden' name = 'pid' value='$prodID'>";
print"<input type='button' value='Add To Cart'/>";
print"</p></form>";
如果您想使用 GET 方法,请参阅 @Ghost回答
关于php - PHP 中的用户定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997628/