saving group checkbox and option values
i have many check boxes all named features, with different values, how do i collect their values in php and whats the best method to save an repopulate the form when 开发者_Go百科its loaded, so tha it will show previously selected values.
thanks
Checkboxes are are generated like this:
<input type='checkbox' name='mycheck' value="1" /> Unchecked checkbox
<input type='checkbox' name='mycheck' value="1" checked /> Checked checkbox
So you will output them with a code similar to this:
<input type='checkbox' name='<?=$checkName?>' <?=$checked?'checked':''?> value="1" /> <?=$checkLabel?>
To redisplay, you will need to prepare the appropriare variables, according to user's post. You could put all of them in an array, and cycle upon it to generate them all.
Some tricks with checkboxes:
- Unchecked checkboxes are NOT posted, i.e. you won't find them at all in the post. This means you cannot just cycle over the checkboxes. You have to cycle upon your full set of possibilities, and check if that possibility is present (checked) or absent (unchecked). Or, you can unset all the possibilities in your model, and then check only those present in the post.
- It is possible to group them together by using arrays, like this:
<input type='checkbox' name='options[check1]' value="1" /> Unchecked checkbox <input type='checkbox' name='options[check2]' value="1" /> Unchecked checkbox
you will get an array called $_POST['options'] in your POST, with check1 and check2 as your keys.
精彩评论