Hi everyone,
I am making a quiz using forms. The user answers by clicking on radio type buttons e.g.

<input type="radio" name="question1" value="whatever1">
<input type="radio" name="question1" value="whatever2">
<input type="radio" name="question1" value="whatever3">

I collect the answer on the second page using something like

$answer1=$_POST['question1'];

Now the problem: If the user doesn't answer at all i get something like
Notice: Undefined index: question1 in c:\......

What can I do to prevent this?
thanks a lot!

Recommended Answers

All 3 Replies

Hi everyone,
I am making a quiz using forms. The user answers by clicking on radio type buttons e.g.

<input type="radio" name="question1" value="whatever1">
<input type="radio" name="question1" value="whatever2">
<input type="radio" name="question1" value="whatever3">

I collect the answer on the second page using something like

$answer1=$_POST['question1'];

Now the problem: If the user doesn't answer at all i get something like
Notice: Undefined index: question1 in c:\......

What can I do to prevent this?
thanks a lot!

if empty($_POST['question1']){
  die(" Please Select an option.");
}
 
else{
 .....process the form
}

and one quick suggestion:
why not use the css along with the error messages to give a good impression.

http://www.happyjunction.com

if empty($_POST['question1']){
  die(" Please Select an option.");
}
 
else{
 .....process the form
}

You've got a syntax error there.

if (empty($_POST['question1'])) {
  die(" Please Select an option.");
}
 
else{
 //.....process the form
}

Off topic:
Use your signature for links to your site. The mods may be cruel. :)

You've got a syntax error there.

if (empty($_POST['question1'])) {
  die(" Please Select an option.");
}
 
else{
 //.....process the form
}

Off topic:
Use your signature for links to your site. The mods may be cruel. :)

yes thanks for pointing the mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.