PDA

View Full Version : PHP quiz


tony09uk
10-31-2011, 03:16 PM
I am looking to make a PHP quiz. There will be between 4 and 20 question per quiz, each question will have between two and four options. I don't want to add anything to a database.

I have looked around and found two tutorials Here (http://pyrocam.com/simple-php-quiz/) and Here (http://www.developingwebs.net/phpclass/phpquizpage.php) that try to teach you how to do it. But they are not very good. Can anyone please advise.

I am sure its a fairly simple thing to create but my VERY limited skill level is preventing me from achieving what i'm try to do.

edbr
11-02-2011, 08:05 AM
yes its not hard. use forms and php with if conditions
if $_POST['question1'] ==1 echo "that is correct"
else echo "wrong'

tony09uk
11-02-2011, 11:03 AM
Once again i'm guilty of over complicating things. Thanks for that.

I would like to take it a step further that though and achieve this (http://www.elementsclassroom.com/elements_IQ/iqZoom.html) sort of quiz. This is a quiz from my website, but it was put together using quizbox. So I don't get access to any of the code used and would like to be able to write my own.

Im not asking you to do it for me but if you can give me an idea of tutorials you know of or what php commands i need to be looking at I will do my best to work it out.

Many thanks

edbr
11-03-2011, 01:00 AM
actually its done how i suggested, or could be. in the if condition (for correct answer, add a variable say $q1 for question 1 $q2 for question2, make the values 1 if correct 0 if wrong in the else condition then on your result page it can tally up the score $q1+$q2 etc.

tony09uk
11-03-2011, 10:48 AM
Brilliant, thanks ed. I will get onto that over the next week and hopefully have something to show for it.

All you help as been invaluable, thanks again.

tony09uk
11-11-2011, 12:16 PM
can you please advise what ive done wrong.

<form action="quiz.php" method="GET" />
Do you use a terminal?<br/>
<input type="radio" name="q1" value="1" /> Yes<br/>
<input type="radio" name="q1" value="0" /> No<br/>


how many domains do you manage?<br/>
<input type="radio" name="q2" value="0" /> I don't <br/>
<input type="radio" name="q2" value="1" /> 1<br/>
<input type="radio" name="q2" value="2" /> 2<br/>
<input type="radio" name="q2" value="3" /> 3<br/>
<input type="radio" name="q2" value="4" /> 4<br/>
<input type="radio" name="q2" value="5" /> 5 or more<br/>

<input type="hidden" name="done" value="1" />
<input type="submit" value="Submit...." />
</form>
<?php


$lopvar2=$_GET['q2'];
$lopvar1=$_GET['q1'];

$total = $lopvar2 + $lopvar1;
echo "Your total score is $total";

?>

Its telling me lopvar1 + lopvar2 are undefined index, what am I missing?

edbr
11-12-2011, 12:05 AM
it does work although i would use $_post personally. i added eco to show the values

<form action="quiz.php" method="GET" />
Do you use a terminal?<br/>
<input type="radio" name="q1" value="1" /> Yes<br/>
<input type="radio" name="q1" value="0" /> No<br/>


how many domains do you manage?<br/>
<input type="radio" name="q2" value="0" /> I don't <br/>
<input type="radio" name="q2" value="1" /> 1<br/>
<input type="radio" name="q2" value="2" /> 2<br/>
<input type="radio" name="q2" value="3" /> 3<br/>
<input type="radio" name="q2" value="4" /> 4<br/>
<input type="radio" name="q2" value="5" /> 5 or more<br/>

<input type="hidden" name="done" value="1" />
<input type="submit" value="Submit...." />
</form>
<?php


$lopvar2= $_GET['q2'];
$lopvar1= $_GET['q1'];

echo "domain score". $lopvar2."<br>";
echo"terminal score". $lopvar1;


$total = $lopvar2 + $lopvar1;
echo "<br>"."Your total score is $total";

?>

tony09uk
11-15-2011, 08:06 PM
I like your addition, thank you.I would have struggled with that later on. I find im getting an undefined index error.....does it matter or can I ignore it?

edbr
11-15-2011, 11:56 PM
post the error

tony09uk
11-18-2011, 06:18 PM
My new issues!

Problem 1)
Notice: Undefined index: q1 in file\quiz.php on line 18
Notice: Undefined index: q1 in file\quiz.php on line 19
Notice: Undefined index: q1 in file\quiz.php on line 20

Why am I getting undefined index? I have tried setting the variables below the quiz but then I get the message below each question.

Below is what I have done:

<?php
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
?>
<form action="quiz.php" method="POST" />
Do you want to learn php?<br/>
<input type="radio" name="q1" value="1" /> Yes <?php if($q1 == 1) {echo "correct answer";} ?><br/>
<input type="radio" name="q1" value="0" /> No <?php if($q1 == 0) {echo "wrong answer";} ?><br/>

Are you rubbish at php?<br/>
<input type="radio" name="q2" value="1" /> Yes <?php if($q2 == 1) {echo "correct answer";} ?><br/>
<input type="radio" name="q2" value="0" /> No <?php if($q2 == 0) {echo "wrong answer";} ?><br/>

Will you get better?<br/>
<input type="radio" name="q3" value="1" /> Yes <?php if($q3 == 1) {echo "correct answer";} ?><br/>
<input type="radio" name="q3" value="0" /> No <?php if($q3 == 0) {echo "wrong answer";} ?><br/>


<input type="hidden" name="done" value="1" />
<input type="submit" value="Submit...." />
</form>

<?php
$total = $q1 + $q2 + $q3;
echo "<br>"."Your total score is $total";
?>

Problem 2)
I have taken your suggestion of showing the user where they went wrong and tried to take it a step further by showing them next to the question, but i'm struggling. It shows the user "wrong answer" before they start the quiz...obviously I don't want this. How can I solve this problem?....I know php starts counting at 0, so guess that is why it shows what i am asking it to echo, but how do i get around that?

edbr
11-18-2011, 11:44 PM
you are getting that warning as , until the form is sent the variables are not defined. ie there is no value for them. same will happen if a question is omitted.

tony09uk
11-20-2011, 10:06 AM
Ahh good....Im on the right lines of thought then :D Does that mean I can ignore it and it wont matter when i disable the error reports? or should I do something to fix it?