我有一个数字数组,并希望为数组中的每个元素创建一个复选框。按下提交按钮后,将显示所选的复选框。有没有办法做到这一点?
PHP代码:
<?php
$arr = array("1","2","3","4");
//Create a checkbox for each array element
foreach($arr as $index=>$value)
{
echo ' <input type="checkbox" name = "checkb[]"value="'.$value.'">';
}
echo'<input type = "submit" name = "submit" value = "submit">';
if(isset($_POST['submit'])){
foreach($_POST['checkb'] as $checkb)
{
echo $checkb." ";
}
}
?>
答案 0 :(得分:0)
您没有任何form
到post
checkbox
值,请使用form
代码,您可以使用in_array()
复选复选框:
<?php
$arr = array("1","2","3","4");
//Create a checkbox for each array element
foreach($arr as $index=>$value)
{
echo '<form action="" method="post">';
echo ' <input type="checkbox" name = "checkb[]"value="'.$value.'">';
}
echo'<input type = "submit" name = "submit" value = "submit"></form>';
if(isset($_POST['submit'])){
foreach($_POST['checkb'] as $checkb)
{
if(in_array($checkb,$arr))
{
echo '<input type="checkbox" name = "checkb[]"value="'.$checkb.'" checked>';
}
}
}
?>