在运行foreach循环PHP时使用$ _POST方法

时间:2014-02-02 21:26:21

标签: php forms post foreach

我正在努力解决这个问题。我有一个foreach循环:

if(isset($_POST['chkboxes'])) {
  foreach($_POST['chkboxes'] as $chkbox) {
        echo '- '.$chkbox."<br />";
  }
        '<input type="hidden" name="options" id="options" value=" ' . $chkbox . '"/>';
        echo '<br /><input type="submit" name="button" id="button" value="Confirm"/> </form> </td></tr></table>';

正如您从上面所看到的,我试图在我使用的新php页面上有$chkbox值的隐藏输入:

if(isset($_POST['options'])){
    $options = $_POST['options'];
}

我正在尝试显示所有选中的项目,或者在新的php页面上显示$chkbox。另一种形式工作得很好,它只是从这个foreach循环中获取信息,并尝试在新的php页面上正确显示该信息。

以下是我的整个代码:

<?php

require("database.php"); //connect to the database

if(isset($_GET['id'])){ 
    $id = $_GET['id'];
}


$result = mysqli_query($con,"SELECT * FROM menuitem WHERE id='$id' "); 
    if (!$result) {
        printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
        exit();
    }


//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
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="100"></tr>
                        <tr>
                            <td width="22%" valign="top" align="left"><img style="border: #66cc33 5px solid;" src="./menupictures/' .$picturepath . '" height="200" width="200" border="1"/></td>
                            <td width="20%" valign="top" align="left">' . $name . ' <br />$' . $price . '<br /><br />
                               <td valign="top" align="left"> <form id="form1" name="form1" method="post" action="cart.php">
                                    <input type="hidden" name="pid" id="pid" value=" ' . $id . '"/>';


    echo $dynamiclist;
    echo "Your Item Options: <br /><br />";
}

if(isset($_POST['chkboxes'])) {
  foreach($_POST['chkboxes'] as $chkbox) {
        echo '- '.$chkbox."<br />";
        '<input type="hidden" name="options[]" id="options" value=" ' . $chkbox . '"/>';
  }
        echo '<br /><input type="submit" name="button" id="button" value="Confirm"/> </form> </td></tr></table>';
        echo'  <table width="90%"><tr><td width="30%"></td>
        <td width="21%"></td>
        <td><font><a href="irohomepage.php">Cancel This Item</a></font></td></tr></table>';
}
mysqli_close($con); //close the db connection

?>

<html>
<body>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解你,但也许这就是你想要实现的目标

if(isset($_POST['chkboxes'])) 
{
    foreach($_POST['chkboxes'] as $chkbox) 
    {
        echo '- '.$chkbox."<br />";
        '<input type="hidden" name="options[]" id="options" value=" ' . $chkbox . '"/>';
    }
}

echo '<br /><input type="submit" name="button" id="button" value="Confirm"/> </form> </td></tr></table>';

答案 1 :(得分:0)

嗯,您的代码中有一个错误:

你正在检查

$_POST['chkboxes']

何时应该检查

$_POST['options']

因为你已将它们命名为:

name="options[]"

所以 - 在你的html和php代码中使用相同的名称。

相关问题