【发布时间】:2014-05-29 07:12:56
【问题描述】:
我想问,如何将表单中的数据移动到 php 或 jquery 中的数组中,以便在测验结束时作为总结,查看哪些问题是正确的,哪些是错误的。 我还想从数据库中随机抽取 30 个问题。 请帮忙。
index.php
<?php require_once 'config.php';?>
<!DOCTYPE html>
<html>
<head>
<title>Testy Kwalifikacja Wstępna kat. C, CE, D, DE OCK OLSZTYN</title>
<meta charset='utf-8'>
<link rel='stylesheet' href='css/style.css'/>
</head>
<body>
<div id="top-logo">
</div>
<div id="wrapper">
<div id="content">
<?php $response=mysql_query("select * from pytania WHERE id='2' OR id='3' OR id='1'");?>
<form method='post' id='quiz_form'>
<?php while($result=mysql_fetch_array($response)){ ?>
<div id="question_<?php echo $result['id'];?>" class='questions'>
<div class="images">
<img src="img/<? echo $result['obrazek']?>">
</div>
<div class="questions-2">
<h2 id="question_<?php echo $result['id'];?>"><?php echo $result['id'].".".$result['pytanie'];?></h2>
</div>
<div class='align'>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
<label id='ans1_<?php echo $result['id'];?>' for='1'><?php echo $result['odp_a'];?></label>
<br/>
<input type="radio" value="2" id='radio2_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
<label id='ans2_<?php echo $result['id'];?>' for='1'><?php echo $result['odp_b'];?></label>
<br/>
<input type="radio" value="3" id='radio3_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
<label id='ans3_<?php echo $result['id'];?>' for='1'><?php echo $result['odp_c'];?></label>
<br/>
<input type="radio" checked='checked' value="5" style='display:none' id='radio4_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
</div>
<br/>
<input type="button" id='next<?php echo $result['id'];?>' value='Next!' name='question' class='butt'/>
</div>
<?php }?>
</form>
<div id='result'>
<br/>
</div>
<div id="demo1" class="demo" style="text-align:center;font-size: 25px;">00:00:00</div>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/watch.js"></script>
<script>
$(document).ready(function(){
$('#demo1').stopwatch().stopwatch('start');
var steps = $('form').find(".questions");
var count = steps.size();
steps.each(function(i){
hider=i+2;
if (i == 0) {
$("#question_" + hider).hide();
createNextButton(i);
}
else if(count==i+1){
var step=i + 1;
//$("#next"+step).attr('type','submit');
$("#next"+step).on('click',function(){
submit();
});
}
else{
$("#question_" + hider).hide();
createNextButton(i);
}
});
function submit(){
$.ajax({
type: "POST",
url: "ajax.php",
data: $('form').serialize(),
success: function(msg) {
$("#quiz_form,#demo1").addClass("hide");
$('#result').show();
$('#result').append(msg);
}
});
}
function createNextButton(i){
var step = i + 1;
var step1 = i + 2;
$('#next'+step).on('click',function(){
$("#question_" + step).hide();
$("#question_" + step1).show();
});
}
setTimeout(function() {
submit();
}, 50000);
});
</script>
</div>
</div>
</body>
</html>
【问题讨论】:
-
如果我正确理解了您的问题,只需点击此链接即可解决您的问题。api.jquery.com/serializearray
-
我很确定您没有编写该代码。这并没有错,但公然寻求解决方案并不是要走的路。如果您尝试破译代码的作用,您会做得更好
-
请注意 mysql 扩展(提供 mysql_ 函数)自 2012 年以来已被弃用,取而代之的是 mysqli 和 PDO 扩展。强烈建议不要使用它。见stackoverflow.com/questions/12859942/…
标签: javascript php jquery arrays forms