Old*_*est 3 php arrays checkbox while-loop
我试图让我的复选框值保存在我动态创建的输入上,而我的失败是悲惨的.是的我在线阅读了十几个或更多的教程,但我找不到一个迎合动态复选框列表的...请设置我的海峡!
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<?php
$checkbox[] = array();
while($row = mysql_fetch_array($result)) {
$checked = isset($_POST['checkbox']) ? " checked" : "";
echo "<input name=\"checkbox[]\" type='checkbox' value='" . $row['first_name'] . "' $checked /> ";
echo $row['first_name'];
echo "<hr />";
//print_r( $_POST['checkbox']);
}
if(isset($_POST['checkbox']) && !empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $checkbox){
echo $checkbox . "<br />";
} }
?>
Run Code Online (Sandbox Code Playgroud)
干得好!!
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<?php
$chvalues = array();
if(isset($_POST['checkbox']))
{
foreach($_POST['checkbox'] as $ch => $value)
{
$chvalues[] = $value;
}
}
while($row = mysql_fetch_array($result))
if(in_array($row['first_name'], $chvalues))
{
echo "<input name=\"checkbox[]\" type='checkbox' value='" . $row['first_name'] . "' checked='checked'/> ";
}
else
{
echo "<input name=\"checkbox[]\" type='checkbox' value='" . $row['first_name'] . "'/> ";
}
echo $row['first_name'];
echo "<hr />";
}
if(isset($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $checkbox => $val){
echo $checkbox .':'.$val."<br />";
}
?>
</form>
Run Code Online (Sandbox Code Playgroud)