开发者

saving group checkbox and option values

开发者 https://www.devze.com 2023-01-04 08:13 出处:网络
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

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:

  1. 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.
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消