Page 1 of 1

If no radio btn is selected, get Undefined Index error

Posted: Wed Mar 24, 2004 9:24 am
by pbritten
The form contains a typical radio button group with none checked. The values in the form are passed to a PHP handler page.

Problem: if none of the radio buttons are selected, the following error is displayed:

Notice: Undefined index: ServDogType in C:\web\Keystone\www2\ssd\prelimappHandler.php on line 68

This is the code from line 68:

$ServDogType = trim($_POST["ServDogType"]);

All of the other form values are accepted. As well, if a radio button IS checked, no error is displayed.

Any ideas, good people?

Thanks very much for your help!

Pat

Posted: Wed Mar 24, 2004 9:33 am
by magicrobotmonkey
state, college, huh?

I think if hte radio button is unchecked, it posts nothing, so the index in the $_POST array which you are referring too does not exist. Verify the $_POST data. Either check it with isset or use javascript to ensure the form is not posted until all items are filled out.

a quick fix is to place the above line in an if block:
if(isset($_POST["ServDogType"]){
$ServDogType = trim($_POST["ServDogType"]);

}

Success

Posted: Wed Mar 24, 2004 9:42 am
by pbritten
Thank you, Magic. It worked like a charm.

Posted: Wed Mar 24, 2004 9:54 am
by magicrobotmonkey
yes, but did you understand why?

Posted: Wed Mar 24, 2004 9:55 am
by pickle
FYI: You can also set the a radio button to be selected by default so the POST variable will always be set.

Understood

Posted: Wed Mar 24, 2004 9:58 am
by pbritten
Magic: yes, I understand.

Thanks again!