Re: Type of form element
| From: | Marcus Bointon | Date: | Sun, 30 Oct 2005 11:52:24 +0000 |
| Subject: | Re: Type of form element | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-224840@lists.php.net to get a copy of this message | ||
On 29 Oct 2005, at 20:59, Richard Lynch wrote:
So you will most likely be using isset($_POST['checkbox_name']) rather than testing for "on"I classify using isset for checking for the existence of array keys to be a bad habit as in some common cases it will not work as you expect, for example: <input type="checkbox" name="checkbox_name" /> (note it has no value attribute) If it's checked, you would get the equivalent URL of "...?checkbox_name=", so $_REQUEST['checkbox_name'] exists, but may contain NULL, and isset would return false even though it's there. A completely reliable check that will never generate any warnings is: array_key_exists('checkbox_name', $_REQUEST). If you have several checkboxes in an array (using names like name="checkbox_name[option1]"), you would say: if (array_key_exists('checkbox_name', $_REQUEST) and is_array($_REQUEST['checkbox_name'])) {
if (array_key_exists('option1', $_REQUEST['checkbox_name'])) {
echo "you selected option 1\n";
}
if (array_key_exists('option2', $_REQUEST['checkbox_name'])) {
echo "you selected option 2\n";
}
//etc...
}
Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk