1

Yup. This one is tearing me up. I found a lot of tutorials on this specific subject, but nothing seems to be working for me. I must be missing something which I cannot find.

I am simply trying to keep selected checkboxes checked if there is a previous error in the form. My validation is working totally fine with all fields, but I need to retain the checked boxes and I just can't solve this.

Here are the snippets I am working with:

This checks if the $data[id] isset()

<input name="seminar[]" type="checkbox" id="seminar[]" 
value="
<?php
if(isset($data['id'])) {
         $checked = "checked=\"checked\"";
      } else {

        echo "";
      }
?>
" <?php echo "$checked"; ?>>

And I even checked in my html source, and this is what is showing after submission:

<input name="seminar[]" type="checkbox" id="seminar[]" value="" checked="checked"> 

As you can see value="" is empty!, so in my script above it should echo "", but that's not working.

As a matter of fact, when I clear the cache and reload the form all boxes are just checked!?

Can anyone see what I am doing wrong. Maybe I am turning over the wrong stones here.. Dunno, but nearly 4 hours down the drain and I'm waiving a white flag.

7
  • php code is not showing. I guess its a moderated thing here : ) Commented Jan 17, 2011 at 20:32
  • 1
    You need to select the PHP and click the {} button to make it show properly. Commented Jan 17, 2011 at 20:33
  • I just fixed it up. In order for code to show up, you need to just indent it by four spaces. Commented Jan 17, 2011 at 20:34
  • 1
    @OldWest: I hope it is fixed now and didn't omit anything. crimson_penguin is right on how to format it. Commented Jan 17, 2011 at 20:35
  • 1
    Sorry, what? PHP is doing things right. What do you expect PHP to output? Based on the output, $data['id'] is set and then value will be outputted "" and it will be checked. Commented Jan 17, 2011 at 20:36

1 Answer 1

3

I see several things wrong that could possibly be contributing to your problem.

First, you're assigned the name of the checkbox seminar[]. This creates an array with values from all checkboxes that use that name. Since you're not specifying a key in between the [], it automatically does this for you. You should specify some sort of key yourself so you can match them up later.

Example:

<input type="checkbox" name="seminar[0]" id="seminar_0" value="" />

Then you can check if it's set by doing this:

if(isset($_POST['seminiar'][0])) {
  echo "checked=\"checked\"";
}

Second, you can't use seminar[] for the ID of the checkbox. Look at my above example for an alternative way you can do it. IDs have to be unique on the page; You cannot use the same ID twice on a single page.

Third, what is this $data['id'] exactly? You should be checking the $_POST value for the exact name of the item. Again, look at my first example.

I'd reformat your code to look something like this:

$checkboxes = array(0 => "Value 1", 1 => "Value 2", 2 => "Value 3");
foreach($checkboxes as $k => $v) {
  echo '<label for="seminar_'.$k.'">'.$v.'</label><input type="checkbox" name="seminar['.$k.']" id="seminar_'.$k.'" value="'.htmlentities($v).'" '.(isset($_POST['seminar'][$k]) ? 'checked="checked" ' : '').'/>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Michael, your comment was very helpful : ). This is what I did, and it's all working now: <?php if(isset($_POST['seminar']["$a"])) { $checked = "checked=\"checked\""; } else { $checked = ""; } ?> <input name="seminar[<?php echo $a; ?>]" type="checkbox" id="seminar_<?php echo $a; ?>" value="" <?php echo $checked; ?>>. , but when I am trying to insert into my table using (existing code) this no longer works: if($seminar) { foreach ($seminar as $s ) $interest .= "$s "; }. any idea how I can process this new array structure based on a foreach()?
Glad I can help. One thing, you don't need double quotes around $a in isset($_POST['seminar']["$a"]). Just do isset($_POST['seminar'][$a]).
Cool. Thanks for the added tip :-) .. I am going to create a new thread to my other question as it's somewhat a different topic.

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.