...

View Full Version : Form Question



haseeb
08-18-2012, 02:30 PM
Iam working on a MCQ script in php but having an error while everything looks fine to me.But when i leave the forms blank and submit i get an error.
here is the code

<?php
// Question 1 //

$question1 = $_GET['q1a'];
if($question1=='a')
{
echo "Your answer to question 1 was correct!";
}
if(is_null($question1))
{
echo "You didn't answer question 1.";
}
elseif($question1 != 'a')
{
echo "Your answer to question 1 was incorrect.";
}

// Question 2 //

$question2 = $_GET['q2a'];

if($question2=='a1')
{
echo "Your answer to question 2 was correct!";
}
if(is_null($question2))
{
echo "You didn't answer question 2.";
}
elseif($question2 != 'a1')
{
echo "Your answer to question 2 was incorrect.";
}


?>

<body>



<form method="GET" action="go.php">
1 - Which is the most visited Website in the year 2010 according to a survey?<br>
<input type=radio name='q1a' value='a'>Google<br>
<input type=radio name='q1a' value='b'>Facebook<br>
<input type=radio name='q1a' value='c'>Amazon<br>
<input type=radio name='q1a' value='d'>Yahoo<br>
<br>
<br>
<br>
2 - What does PHP stand for ?<br>
<input type=radio name='q2a' value='a1'> Private Home Page<br>
<input type=radio name='q2a' value='b1'>PHP: Hypertext Preprocessor<br>
<input type=radio name='q2a' value='c1'>Personal Hypertext Processor<br>
<input type=radio name='q2a' value='d1'>Personal Home Page<br>
<input type=submit value='Go'>



</form>
</body>
</html>

And the error i get is this

( ! ) Notice: Undefined index: q1a in C:\webserver\www\test\go.php on line 40
Call Stack
# Time Memory Function Location
1 0.0351 367440 {main}( ) ..\go.php:0
You didn't answer question 1.

( ! ) Notice: Undefined index: q2a in C:\webserver\www\test\go.php on line 56
Call Stack
# Time Memory Function Location
1 0.0351 367440 {main}( ) ..\go.php:0
You didn't answer question 2.

AndrewGSW
08-18-2012, 02:44 PM
You need to check if the user actually answered the question:


if (isset($_GET['q1a'])) {
// they answered the question
}

BTW The answer to question 2 is either b1 or d1. But b1 is perhaps more correct than d1 ;)

haseeb
08-18-2012, 02:58 PM
It is still not working getting the error again.For checking that user have answered or not i used.
if(is_null($question2))
Lol the answer to question 2 is b i just copy pasted the first one.

djm0219
08-18-2012, 03:24 PM
It is still not working getting the error again.For checking that user have answered or not i used.
if(is_null($question2))

Heed the advice AndrewGSW gave you. You need to test whether or not a question was answered before you try to retrieve the value.



$question1 = '';
if (isset($_GET['q1a'])) {
$question1 = $_GET['q1a'];
}
if($question1=='a') {
echo "Your answer to question 1 was correct!";
} elseif ($question1 != 'a') {
echo "Your answer to question 1 was incorrect.";
} else {
echo "You didn't answer question 1.";
}

AndrewGSW
08-18-2012, 04:29 PM
Lol the answer to question 2 is b i just copy pasted the first one.

So if I answered d would I receive no marks? :p

DrDOS
08-18-2012, 07:58 PM
This code could be 'smartened up' in several ways. One would be to use javascript to see if any of the entries were made. Another would be to make a fifth default entry that would automatically be chosen or checked, something like "No choice made.'. Neither radio or checkbox entries return anything to the server if they aren't checked. Another way might be to use select boxes, a little more complex but foolproof.

haseeb
08-19-2012, 12:21 AM
Thanks for the help and andrew and djm0219 its working now i simplified it more. Iam new to php iam just learning php by creating little simple scripts.

// Question 1 //

$question1 = '';
if (isset($_GET['q1a'])) {
$question1 = $_GET['q1a'];
}

if($question1=='a')
{
echo "Your answer to question 1 was correct!<br>";
}
if (trim($question1) == '') {
echo 'Please choose answer';
}
elseif($question1 != 'a')
{
echo "Your answer to question 1 was incorrect.<br>";
}
// Question 2//
$question2 = '';
if (isset($_GET['q2a'])) {
$question2= $_GET['q2a'];
}

if($question2=='b')
{
echo "Your answer to question 1 was correct!<br>";
}
if (trim($question1) == '') {
echo 'Please choose answer';
}
elseif($question1 != 'b')
{
echo "Your answer to question 1 was incorrect.<br>";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum