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 />";
} }
?>
2
  • can you show your generated html before posting?? Commented Apr 19, 2011 at 5: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. stackoverflow.com/a/4559976/2943403 Commented Aug 19, 2021 at 2:53

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

7 Comments

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, well the idea is pretty much the same, could you post your generated html code?? perhaps we can get some clue from there?
<form action="/php/checkboxes_save_value.php" method="post" name="checkboxForm"> Array ( [0] => Mike [1] => Alycia ) <input name="checkbox[]" type='checkbox' value='Mike' /> Mike<hr />Array ( [0] => Mike [1] => Alycia ) <input name="checkbox[]" type='checkbox' value='Alycia' /> Alycia<hr />Mike<br />Alycia<br /><input name="submit" type="submit" value="Send Checkbox Data &gt;&gt;" /> Im doing a print_r for the results. </form>
I can't believe this! This is not even working! foreach($_POST['checkbox'] as $checkbox){ // echo $checkbox . "<br />"; $checked = $checkbox != "" ? " checked" : ""; } while($row = mysql_fetch_array($result)) { echo "<input name=\"checkbox[]\" type='checkbox' value='" . $row['first_name'] . "' $checked /> "; echo $row['first_name']; echo "<hr />"; //print_r( $_POST['checkbox']); } both checkboxes are checked every time I submit IF only one checkbox is selected???
#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.
|
0
$checked = isset($_POST['checkbox']) && in_array($row['first_name'], $_POST['checkbox']) ? " checked" : "";

2 Comments

Gaurav, I will try this in the morning. I like this approach as well it's much less code than I had originally.
While compact, this one-line answer is missing its educational explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.