Reputation: 285
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
Upvotes: 1
Views: 1391
Reputation: 2820
Change this:
foreach($size as $size_available)
for this:
foreach($_REQUEST['size'] as $size_available)
There's no need for the unserialize command.
Upvotes: 0
Reputation: 27599
Validate that the index is set before accessing it with isset()
-
<? echo (isset($checked['8 w']))? $checked['8 w'] : "" ?>
Upvotes: 1