# validating checkbox values in php



## sudhakararaog (Sep 6, 2007)

Hi

my question is about retrieving value from a checkbox in php. Lets say a html page has 2 check boxes with the same name attribute defined as an array ex: 
Now if even one or both of them are selected i can easily retrieve which one has been checked and its value, however the problem i am facing is, if a user does not select any of the check boxes or even radio buttons and clicks the submit button I get an error message "Undefined index " followed by the name of the checkbox which was not ticked. Can someone please provide the solution along with the code. I guess the same code can also be used for radio button as the error occurs when a user does not click any radio buttons. Can i pass variables using javascript or is there any function in PHP. Any help received in this regard will be greatly appreciated.


----------



## Menekali (Sep 6, 2007)

Something like this suit your needs?


```
Please Check Me
```


----------



## sudhakararaog (Sep 6, 2007)

thanks a lot for your reply. in my situation if assuming the user does not click the checkbox or even a radio button i should be able to retrieve the value as ex: "unchecked" which i can assign to a variable. But in the php file it is not able to read that the checkbox or radio button has been unchecked and due to this there is an error message = "Undefined Index" and the filename.php How to avoid this error and still be able to read that the checkbox or radio button has not be checked. Please advice.


----------



## ehymel (Aug 12, 2007)

Sounds like the problem has to do with trying to parse your results array when one does not exist. You might try testing whether any results exist first. Possible tests could be array_key_exists() or isset(). See http://us3.php.net/manual/en/function.array-key-exists.php and http://us3.php.net/manual/en/function.isset.php.


----------



## MMJ (Oct 15, 2006)

@Menekali: The <script> tag should look like this: <script type="application/javascript">


----------



## ehymel (Aug 12, 2007)

Either one works equivalently in most browsers, although technically MMJ is correct as <script language="javascript"> has been deprecated.


----------



## MMJ (Oct 15, 2006)

ehymel said:


> Either one works equivalently in most browsers, although technically MMJ is correct as <script language="javascript"> has been deprecated.


True. If a browser didn't support old standards it would get a very big user base.


----------

