开发者

Maintain checkbox value with dynamically created checkboxes and array

I am trying to get my checkbox value to save on my dynamic created input, and I am failing miserably. Yes I've read a dozen or more tutorials online, but I cant find one that caters to a dynamic checkbox list... Please set me strait!!

<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 "<i开发者_运维知识库nput 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 />";
} }
?>


HERE you go!!

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


$checked = isset($_POST['checkbox']) && in_array($row['first_name'], $_POST['checkbox']) ? " checked" : "";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜