这是我的代码:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="20"></tr>
<tr>
<td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br />
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value=" ' . $name . ' - $' . $price . ' - "checkbox"/>';
echo $dynamiclist;
foreach(explode(',', $keywords) as $keyword) {
$keyword = trim($keyword);
echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword <br /><br />";
}
echo '<input type="submit" name="button" id="button" value="Add to Order"/> </form> </td></tr></table>';
}
mysqli_close($con); //close the db connection
代码使用项目 ID 提取项目,然后为该项目创建复选框和关键字。用户单击所需的复选框选项,然后提交将信息发送到购物车的表单。问题是,提交表单时,仅发送商品的名称和价格,而不发送单击的复选框选项。你能帮我找出原因吗?边注。在我的数据库中,我有一列“关键字”,其中存储了多个关键字。//五分熟、好、稀、牛奶、苹果汁、薯条。另外,在 cart.php 上我有:
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
它使用复选框从脚本发布数据。我只是被困住了,无法再继续了。
最佳答案
您的复选框应该有不同的名称:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="20"></tr>
<tr>
<td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br />
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value=" ' . $name . ' - $' . $price . ' - "checkbox"/>';
echo $dynamiclist;
$i=0;
foreach(explode(',', $keywords) as $keyword) {
$keyword = trim($keyword);
$chkname = "checkbox{$i}";
$i = $i+1;
echo "<input type='checkbox' name='$chkname' value='$keyword'>$keyword <br /><br />";
}
echo '<input type="submit" name="button" id="button" value="Add to Order"/> </form> </td></tr></table>';
}
mysqli_close($con); //close the db connection
您需要在 cart.php 中获取复选框数据,如下所示:
if($_REQUEST['checkbox0']) echo $_REQUEST['checkbox0'];
if($_REQUEST['checkbox1']) echo $_REQUEST['checkbox1'];
//so on foreach checkbox
关于php - 使用 PHP 和 MySQL 保存复选框中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20861575/