April 30, 2012

Multiple-choice picture quiz for young students

Question by Sean

I’m trying to make a quiz with multiple submit buttons. I want a picture to appear with ten buttons. Each time the test-taker presses a button I want the value to be posted and the picture to change. So far I’ve tried three approaches:-

1.Using onclick and javascript I could get all the animation done but the values didn’t get posted. From Google I get the impression I’d have to use more javascript to submit the values?

2.Using variables in php the first picture would show and first value would post but then nothing happened, adding a loop meant that all the pictures appeared on top of the other without waiting for any buttons to be clicked…

3.I tried doing separate html pages for each picture, the value then gets posted to a php file, which does $score++; and goes to the next html file, but then the $score isn’t right. Because it’s a local variable? Would this mean I’d have to hold the value in MySQL to get at it? Also this seems a very wasteful way to program the whole thing…

Here is the code for no. 2:

<?php
include 'header.php';

if ($round==1) {$ans='Banana';}
if ($round==2) {$ans='Book';}
if ($round==3) {$ans='Pencil';}
...and so on

if ($_POST['submit']==$ans) {$score++; echo "Right!"; $round++; }
else
{$round++;}
?>

<html>
<form action="index.php" method="post">

<input type="submit" class="button1"  name="submit" value="Banana">
<input type="submit" class="button2" name="submit" value="Balloon">
<input type="submit" class="button3" name="submit" value="Dog">
<input type="submit" class="button4" name="submit" value="Mouse">
<input type="submit" class="button5" name="submit" value="Chair">
<input type="submit" class="button6" name="submit" value="Twelve">
<input type="submit" class="button7" name="submit" value="Pen">
<input type="submit" class="button8" name="submit" value="Book">
<input type="submit" class="button9" name="submit" value="Ball">
<input type="submit" class="button10" name="submit" value="Elephant">
</form>
</html>

<?php

if($ans=="Banana")
{   
echo '<img src="Banana.png" id="picture" class="picture" height="600" width="600" alt="script.aculo.us" />';
}
if($ans=="Pencil")
{   
echo '<img src="Pencil.png" id="picture" class="picture" height="600" width="600" alt="script.aculo.us" />';
}
{   
echo '<img src="Book.png" id="picture" class="picture" height="500" width="500" alt="script.aculo.us" />';
}
...and so on

include 'footer.php';

?>

Any help would be greatly appreciated 🙂

Answer by Starx

Among the approaches, the third one is better than others. Generally on MCQ pages, the question are kept or separate pages. The problem with your local variable can you solved if you use sessions

$_SESSSION['score'] += $score
echo $_SESSION['score'];

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!