Hi All, I have a checkbox in HTML page. <input type="checkbox" name="searchMe"> Code (markup): When I submit the form and retrieve the value in PHP $strSearchMe = $_REQUEST['searchMe']; Code (markup): If the checkbox is checked then its working fine but If it's not checked then I am getting an error Notice: Undefined index: searchMe in C:\Inetpub\wwwroot\jobhob\registeremployers.php on line 28 I dont understand why this error is coming. I would appreciate any help in this regard. Thanks in advance! Best Regards, Inderpal Singh
I think it's because the array element 'searchMe' is not set if the checkbox is not selected. Use the isset() function to see if it has been set, like this: if (isset($_REQUEST['searchMe'])) { $strSearchMe = $_REQUEST['searchMe']; } else { $strSearchMe = false; } PHP: Cheers, Cryo.
Cryogenius is correct: it's part of the HTML spec that checkbox values are not sent if they are not checked. It's not a problem with PHP as such, it's a 'problem' with the HTML spec.
You can also omit warning by using @: $strSearchMe = @$_REQUEST['searchMe']; PHP: If checkbox is not checked, $strSearchMe will be empty.
I'm doing something like this : $value=$_REQUEST["checkbox"]; if ((!$value) || ($value=="") ) $value=0; PHP:
I usually use the following trick. <input type="hiddden" name="searchMe" value="0"> <input type="checkbox" name="searchMe" value="1"> HTML: Only the last value is sent, so if the checkbox is checked 1 is sent, otherwise 0 is sent. Simplifies the PHP as there's always a value.