php - PHP 中的用户定义按钮

标签 php html mysql forms form-submit

所以我正在尝试制作一个购物车。我需要做的是将用户想要购买的产品 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/

相关文章:

php - 我怎样才能用 div 设置这个 php 的样式?

php - 如何在php中获取选定表项的id

javascript - 找到页面最后一个div,调用它的点击函数

css - 将包含联系表的 div 居中

html - 在特定列加载 html 表

mysql - 在查询结果中添加空日期行

php - 如何在我的 php 页面中应用反向 ajax 来重新加载我列出的项目?

javascript - onclick,删除未定义

mysql - 根据特定条件显示总计

python - 如何从 mysql 查询运行 python 脚本?