3

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 "<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 />";
} }
?>
OldWest
  • 2,355
  • 7
  • 41
  • 61
  • can you show your generated html before posting?? – S L Apr 19 '11 at 05:21
  • For the record, `isset($_POST['checkbox']) && !empty($_POST['checkbox'])` is an anti-pattern that shouldn't exist in anyone's code for any reason. https://stackoverflow.com/a/4559976/2943403 – mickmackusa Aug 19 '21 at 02:53

2 Answers2

6

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>
S L
  • 14,262
  • 17
  • 77
  • 116
  • I see what you are doing, but part of the problem is the value needs to be retained as the first_name value. I can't use counter numbers. – OldWest Apr 19 '11 at 05:26
  • @OldWest, well the idea is pretty much the same, could you post your generated html code?? perhaps we can get some clue from there? – S L Apr 19 '11 at 05:27
  • Array ( [0] => Mike [1] => Alycia ) Mike
    Array ( [0] => Mike [1] => Alycia ) Alycia
    Mike
    Alycia
    Im doing a print_r for the results.
    – OldWest Apr 19 '11 at 05:34
  • I can't believe this! This is not even working! foreach($_POST['checkbox'] as $checkbox){ // echo $checkbox . "
    "; $checked = $checkbox != "" ? " checked" : ""; } while($row = mysql_fetch_array($result)) { echo " "; echo $row['first_name']; echo "
    "; //print_r( $_POST['checkbox']); } both checkboxes are checked every time I submit IF only one checkbox is selected???
    – OldWest Apr 19 '11 at 05:44
  • #experimentX, that did the trick. There was a misplaced bracket and some other minor items I had to adjust, but this solved the issue as far as I can tell. Thanks for the persistence.. My mind was pretty much melted. – OldWest Apr 19 '11 at 05:57
  • @OldWest sure glad to help you .. i couldn't test because I didn't have the database. – S L Apr 19 '11 at 05:58
  • This answer is unnecessarily verbose. You only need to conditionally print `checked='checked'` not the full html markup. This answer is missing its educational explanation. – mickmackusa Aug 19 '21 at 02:54
0
$checked = isset($_POST['checkbox']) && in_array($row['first_name'], $_POST['checkbox']) ? " checked" : "";
Gaurav
  • 28,447
  • 8
  • 50
  • 80