我正在编写一个简单的php html代码,从数据库中获取产品信息,每个产品都有一个提交按钮,其值必须是产品的id,如下所示:
<input type="submit" name="productId" value="' .$row['produc_id'] . '" />
在下一页,我可以知道用户选择的产品id使用这个:
$productId = $_POST['productId'];
上面的代码将工作得很好,但问题是我不能在提交按钮中放入任何文本,文本如购买或添加到购物车,所有提交按钮都有一个数字值,这是产品的id。
我试过这个代码,但它没有工作:
<input type="submit" value=" buy " />
<input type="hidden" name="productId" value="' .$row['produc_id'] . '" />
您可以尝试这样做:
<form action="someaction.php" method="post">
<button type="submit" value="12" name="productId">Submit ME</button>
<button type="submit" value="13" name="productId">Submit Too</button>
</form>
确保您的输入包含在表单标记中。
<form method="POST" action="index.php">
<input type="hidden" name="form-product" value="PRODUCT_ID" />
<input type="submit" name="form-submit" value="SEND" />
</form>
然后,您可以使用isset()来检查表单是否已提交,如果已提交,则使用已提交的隐藏输入字段中的信息。
<?PHP
if (isset($_POST['form-submit']))
echo "Product ID: ".$_POST['form-product'];
?>
供参考:我注意到你输入了:$row['produc_id']
,这可能是一个打字错误,它应该是:$row['product_id']
(在producT中缺少一个t)。