针对年轻学生的多项选择图片测验

时间:2012-04-26 10:54:08

标签: php javascript html

我尝试使用多个提交按钮进行测验。我想要一张带有十个按钮的图片。每次测试者按下按钮时,我希望发布值并更改图片。到目前为止,我已经尝试了三种方法: -

1.使用onclick和javascript我可以完成所有动画但是值没有发布。从Google我得到的印象是我必须使用更多的javascript来提交值?

2.在php中使用变量,第一张图片会显示,第一张值会发布,但之后什么也没发生,添加一个循环意味着所有图片都显示在另一张图片的顶部而不等待点击任何按钮... < / p>

3.我尝试为每张图片分别创建html页面,然后将值发布到php文件中,这样可以获得$ score ++;并转到下一个html文件,但然后$得分是对的。因为它是一个局部变量?这是否意味着我必须保持MySQL的价值才能获得它?这对整个事情的编程似乎是一种非常浪费的方式......

这是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';

?>

非常感谢任何帮助:)

3 个答案:

答案 0 :(得分:0)

在你的情况下优雅的解决方案是使用javascript(使用onClick)和AJAX(一个支持jQuery的AJAX查询的库 - http://api.jquery.com/category/ajax/).Every用户点击选择按钮的时间,你应该更新javascript中的得分值和请求新图像的网址及其正确答案。当用户点击最后一个按钮发送时,您应该发送最终得分。  有关AJAX的更多详细信息,请访问:http://www.w3schools.com/ajax/default.asp  希望它有所帮助

答案 1 :(得分:0)

在这些方法中,第三种方法比其他方法更好。通常在MCQ页面上,问题是保留或单独的页面。如果您使用 会话

,您可以解决本地变量的问题
$_SESSSION['score'] += $score
echo $_SESSION['score'];

答案 2 :(得分:0)

我希望每个按钮都将其值提交给一个函数(onclick =“answers('pencil')”)

然后,我会得到一个正确答案顺序的数组,以及一个用户周围的计数器整数。在已回答的函数中,我会将提交的值与位于'round'的答案数组中的项目进行比较(可能会减去一,取决于您想要计算的方式)并且可能会增加猜测次数(如果这就是您的评分)< / p>

当两个值匹配时,递增计数器,并更改图像的src属性。如果没有,请显示一些“再次猜测”消息

当最后一轮完成后,您可以将猜测计数发布到您的服务器进行存储(如果这是您的意图)

这是一些示例代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<div id="buttons">
<button onclick="answered('banana')">Banana</button>
<button onclick="answered('balloon')">Balloon</button>
<button onclick="answered('dog')">Dog</button>
<button onclick="answered('mouse')">Mouse</button>
<button onclick="answered('chair')">Chair</button>
<button onclick="answered('twelve')">Twelve</button>
<button onclick="answered('pen')">Pen</button>
<button onclick="answered('book')">Book</button>
<button onclick="answered('ball')">Ball</button>
<button onclick="answered('elephant')">Elephant</button>
</div>
<div id="msg"></div>
<img id="photo" />

</body>
<script type="text/javascript">
var turn=0;
var guess=0;
//this is the order of correct answers
var answers = ['banana','book','elephant','dog','pen','balloon','mouse','ball','chair','twelve'];
//these are the image urls (yours would be more like 'Pen.png'
var images= ['http://caloriesinbanana.net/wp-content/uploads/2012/01/Calories-in-Banana.jpeg','http://www.instructables.com/image/FZJO197FB11U3O8/Write-a-good-book.jpg','http://images.nationalgeographic.com/wpf/media-live/photos/000/004/cache/african-elephant_435_600x450.jpg','http://1.bp.blogspot.com/-pKym47-kmsk/Trj8aES5iRI/AAAAAAAADII/5tUPH7YZUnw/s1600/Dog.jpg','http://tacticalpens.net/wp-content/uploads/2011/05/surefire-tactical-pen.jpg','http://1.bp.blogspot.com/-zLp3E7kSKm4/TV6W5FNY44I/AAAAAAAAAy8/lvievPXdh68/s1600/red+balloon.jpg','http://kids.mdbc.gov.au/__data/page/735/Pilliga_Mouse.JPG','http://thelatesthiss.org/wp-content/uploads/2012/03/soccer-ball1.png','http://remodelista.com/img/sub/uimg//02-2011/ikea-olle-chair-red.jpg','http://ctmls.ctreal.com/wp-content/uploads/2011/12/twelvekillerblogposts.jpg'];

document.getElementById('photo').setAttribute('src',images[turn]);
function answered(a){
    guess++;
    if(a==answers[turn]){
        turn++;
        if(turn!=answers.length){
            document.getElementById('msg').innerHTML='<span style="color:#FFF;background-color:#0F0;">Great Job!!</span>';
            setTimeout(clr,1000);
            document.getElementById('photo').setAttribute('src',images[turn]);
        }
        else{
            alert('You finished the quiz. You were right '+answers.length+'/'+guess+' times for a score of '+Math.round(100*answers.length/guess)+'%');
        }
    }
    else{
        document.getElementById('msg').innerHTML='<span style="color:#FFF;background-color:#F00;">Guess Again</span>';
        setTimeout(clr,2000);
    }
}

function clr(){
    document.getElementById('msg').innerHTML='';
}

</script>
</html>
相关问题