Re: Type of form element

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxx | http://www.synchromedia.co.uk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux