gpt4 book ai didi

php - 使用动态创建的复选框和数组维护复选框值

转载 作者:可可西里 更新时间:2023-11-01 00:36:47 24 4
gpt4 key购买 nike

我试图让我的复选框值保存在我动态创建的输入上,但我失败得很惨。是的,我已经在线阅读了十几本或更多教程,但我找不到适合动态复选框列表的教程...请让我直截了当!!

<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 />";
} }
?>

最佳答案

给你!

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

关于php - 使用动态创建的复选框和数组维护复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5711886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com