打印复选框的值

时间:2014-12-22 11:17:03

标签: php checkbox foreach

if (isset($_POST['approve']))
{
    $con=mysql_connect("localhost","root","");
    mysql_select_db("shoolin",$con);
    $arr=$_POST['check'];
    foreach($arr as $selected)
    {
    echo $selected."</br>";
    }
}

我需要打印已选中复选框的值,所以请提出一些解决方案来解决复选框打印值的问题

此代码显示foreach循环中的错误

Invalid argument supplied for foreach() in C:\xampp\htdocs\xampp\user.php on line 8

2 个答案:

答案 0 :(得分:2)

试试这种方式..

<form action="#" method="post">
    <input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
    <input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
    <input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
    <input type="submit" name="submit" value="Submit"/>
</form>
<?php
    if (isset($_POST['submit'])) { //to run PHP script on submit
        if (!empty($_POST['check_list'])) {
            // Loop to store and display values of individual checked checkbox.
            foreach ($_POST['check_list'] as $selected) {
                echo $selected."</br>";
            }
        }
    }
?>

答案 1 :(得分:0)

错误是$ arr不是数组

if (is_array($arr))
{
    foreach ($arr as $selected)
    {
        echo $selected."</br>";
    }
}
else
{
    echo $arr."</br>";
}