If no radio btn is selected, get Undefined Index error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pbritten
Forum Newbie
Posts: 13
Joined: Wed Mar 24, 2004 9:24 am
Location: State College, Pennsylvania, USA

If no radio btn is selected, get Undefined Index error

Post 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
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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"]);

}
pbritten
Forum Newbie
Posts: 13
Joined: Wed Mar 24, 2004 9:24 am
Location: State College, Pennsylvania, USA

Success

Post by pbritten »

Thank you, Magic. It worked like a charm.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

yes, but did you understand why?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

FYI: You can also set the a radio button to be selected by default so the POST variable will always be set.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
pbritten
Forum Newbie
Posts: 13
Joined: Wed Mar 24, 2004 9:24 am
Location: State College, Pennsylvania, USA

Understood

Post by pbritten »

Magic: yes, I understand.

Thanks again!
Post Reply