1

I have a bunch of checkboxes when submitted the values (if checked) are added to an array and serialized then saved on my db in one row.

Now I'd like to show the checkboxes on an edit page with the appropriate checkboxes checked.

I'm currently outputting the checkboxes as such...

HTML

6 w <input type="checkbox" name="size[]" value="6 w">
7 w <input type="checkbox" name="size[]" value="7 w">
8 w <input type="checkbox" name="size[]" value="8 w">

I've tried echoing out a checked value but not sure how to get the right ones checked. This was one suggestion from a site I came across...

HTML

6 w <input type="checkbox" name="size[]" value="6 w" <? echo $checked['6 w'] ?> >
7 w <input type="checkbox" name="size[]" value="7 w" <? echo $checked['7 w'] ?> >
8 w <input type="checkbox" name="size[]" value="8 w" <? echo $checked['8 w'] ?> >

PHP

$size = unserialize($row["size"]);        
$array = "";
foreach($size as $size_available)
{
  $array .= "$size_available,";
}
  $array_explode = explode(',',$array);

    //$checked = "";
    foreach ($array_explode as $v){
    $checked[$v] = "checked='CHECKED'";
}

This almost works showing the correct checkboxes checked but any checkboxes not stored in the array give out an undefined index error. I'm guessing because it is outputting an empty variable???

Any suggestions where to go from here or how to do this a better way?

Cheers

2
  • Probably you are getting this error when your $array_explode variable is empty, so before doing foreach you should verify if the array_explode var is empty: if(count($array_explode) > 0) foreach... Use the same method for $size array Commented Sep 24, 2012 at 4:30
  • @MateiMihai You're absolutely right, thanks for the fix. Commented Sep 24, 2012 at 4:48

2 Answers 2

1

Validate that the index is set before accessing it with isset() -

<? echo (isset($checked['8 w']))? $checked['8 w'] : "" ?>
Sign up to request clarification or add additional context in comments.

2 Comments

As other answers pointed out, you should also double check $array_explode = explode(',',$array); - if $array is empty, the call to explode() will return false, and your foreach() will fail.
Yeah did get that and fixed ;)
0

Change this:

foreach($size as $size_available)

for this:

foreach($_REQUEST['size'] as $size_available)

There's no need for the unserialize command.

5 Comments

The issue is with the "undefined index error" when printing the values in the HTML due to some of the keys not being set in the array if they aren't saved in the database. Although the code may not be optimal, the issue is that there is not check for isset() on the array.
@doublesharp Partially agree. It may raise an "undefined index" warning (not error) if ALL the checkboxes are empty. If there is at least 1 checkbox checked it will work. And isset validation will correct this.
I don't think that is where the "undefined index" would originate... it's with the $checked array. I am assuming that it is defined as $checked = array() somewhere not in the example, which may not be a valid assumption so I will update my answer accordingly.
This didn't seem to work for what ever reason so at the moment I've just unserialized it
Only inclusion of $checked is with $checked[$v] = "checked='CHECKED'";

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.