【问题标题】:Using the $_POST method while running foreach loop PHP在运行 foreach 循环 PHP 时使用 $_POST 方法
【发布时间】:2014-02-02 21:26:20
【问题描述】:

我正在努力解决这个问题。我有一个 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>

【问题讨论】:

  • html 是什么样子的?
  • 你的问题是为什么它不回显隐藏的输入字段?
  • 好吧,我想用 if(isset($_POST['options'])){ $options = $_POST['options']; } 然后在新的 php 页面上,我回显了这些信息。我只需要帮助从复选框中提取这些数据。

标签: php forms post foreach


【解决方案1】:

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

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>';

【讨论】:

  • 我试过了,它仍然没有显示复选框。但是,在我的新页面上,当我更改我的: if(isset($_POST['button'])){ $options = $_POST['button']; } 它发布值“确认”
【解决方案2】:

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

你正在检查

$_POST['chkboxes']

什么时候检查

$_POST['options']

因为您已将它们命名为:

name="options[]"

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

【讨论】:

  • 我知道。我只想要被选中的复选框。不过,谢谢你的提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
相关资源
最近更新 更多