[英]how to get a value from radio button in php form and check the value in another php
when i select any radio button on this form in index.php ,the condition for the value in radio button is not validated for the correct answer 当我在index.php中选择此表单上的任何单选按钮时,单选按钮中值的条件未得到正确答案的验证
index.php 的index.php
<html>
<head>Aptitude Test</head>
<body>
<?php require_once 'config.php';?>
<?php $response=mysql_query("select * from questions");?>
<form method='post' id='quiz_form' >
<?php while($result=mysql_fetch_array($response)){ ?>
<div id="question_<?php echo $result['id'];?>" class='questions'>
<h2 id="question_<?php echo $result['id'];?>">
<?php echo $result['id'].".".$result['question'];?>
</h2>
<div class='align'>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>' >
<label id='answer1_<?php echo $result['id'];?>' for='answer1_<?php echo $result['id'];?>'><?php echo $result['answer1'];?></label>
<br/>
<input type="radio" value="2" id='radio2_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>' >
<label id='answer2_<?php echo $result['id'];?>' for='answer2_<?php echo $result['id'];?>'><?php echo $result['answer2'];?></label>
<br/>
<input type="radio" value="3" id='radio3_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>' >
<label id='answer3_<?php echo $result['id'];?>' for='answer3_<?php echo $result['id'];?>'><?php echo $result['answer3'];?></label>
<br/>
<input type="radio" value="4" id='radio4_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>' >
<label id='answer4_<?php echo $result['id'];?>' for='answer4_<?php echo $result['id'];?>'><?php echo $result['answer4'];?></label>
<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'>
<img src='results.jpg' alt='Results'/>
<br/>
</div>
<div id="demo1" class="demo" style="text-align:center;font-size: 25px;">00:00:00</div>
<script src="jquery-1.9.1.min.js"></script>
<script src="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);
});
</body>
</html>
This is ajax.php which gets the selected radio button answer to check the answer is right or wrong, but the thing is the right answer condition if($_POST[$i] == $row['answer']) fails to check and does not increment the value $right_answer++; 这是ajax.php,它获取选定的单选按钮答案以检查答案是对还是错,但是如果($ _POST [$ i] == $ row ['answer'])无法检查,那么事情就是正确的答案条件并且不会增加值$ right_answer ++;
ajax.php ajax.php
<?php require_once 'config.php';
$query = "select id,question,answer from questions";
$result=mysql_query($query) or die(mysql_error());
$right_answer=0;
$wrong_answer=0;
$unanswered=0;
$i=1;
while($row=mysql_fetch_array($result))
{
if($_POST[$i] == $row['answer'])
{
$right_answer++;
}
else if($_POST[$i]==5)
{
$unanswered++;
}
else
{
$wrong_answer++;
}
$i++;
}
echo "<div id='answer'>";
echo " Right Answer : <span class='highlight'>". $right_answer."</span><br>";
echo " Wrong Answer : <span class='highlight'>". $wrong_answer."</span><br>";
echo " Unanswered Question : <span class='highlight'>". $unanswered."</span><br>";
echo "</div>";
?>
config.php file: config.php文件:
<?php
define('DB_HOST','localhost');
define('DB_NAME','Aptitude');
define('DB_USER','root');
define('DB_PASSWORD','');
$conn=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$conn) or die("Failed to connect to MySQL: " . mysql_error());
?>
Please help me to solve this problem. 请帮我解决这个问题。 Thanks in advance. 提前致谢。
This is not a full answer, but it will get you a long way down the road to a solution. 这不是一个完整的答案,但是它将带您走很长一段路,找到解决方案。
You were missing a few elements to your solution, but the most important omission was the javascript/jQuery. 您在解决方案中缺少一些元素,但是最重要的遗漏是javascript / jQuery。
jQuery is a javascript library that makes writing javascript MUCH shorter, simpler and more logical. jQuery是一个JavaScript库,可让编写JavaScript的时间更短,更简单且更具逻辑性。 jQuery can do almost anything that javascript can -- but to use it you must reference the jQuery library in the <head>
tags, as I have done. jQuery几乎可以完成javascript可以做的所有事情-但要使用它,您必须像我一样在<head>
标记中引用jQuery库。
With jQuery you can trap each click of a NEXT button and figure out what question was clicked. 使用jQuery,您可以捕获每个NEXT按钮的单击,并找出单击了哪个问题。 Then, you can run an ajax procedure and get the correct answer for that question. 然后,您可以运行ajax过程并获得该问题的正确答案。
AJAX is a javascript/jQuery procedure that allows you to ask the server to look something up for you, and then send back an answer. AJAX是一种javascript / jQuery过程,可让您要求服务器为您查找内容,然后发送回答案。 You cannot use ajax without javascript, and it is tons simpler to use the $.ajax() methods from jQuery than to use the XMLHTTPRequest methods of pure javascript. 没有JavaScript就不能使用ajax,使用jQuery中的$ .ajax()方法要比使用纯JavaScript的XMLHTTPRequest方法简单得多。
To re-create your situation, I created a database with 7 fields: 为了重新创建您的情况,我创建了一个包含7个字段的数据库:
id
- INT - the question number id
-INT-问题编号 question
- VARCHAR - the question text question
-VARCHAR-问题文本 answer
- VARCHAR - the correct answer answer
-VARCHAR-正确答案 answer1
- VARCHAR - multiple-choice answer #1 答案1- answer1
选择题#1 answer2
- VARCHAR - multiple-choice answer #2 答案2- answer2
选择题#2 answer3
- VARCHAR - multiple-choice answer #3 答案3- answer3
选择题#3 answer4
- VARCHAR - multiple-choice answer #4 答案4- answer4
选择题#4 FILE 1: 文件1:
<?php
include 'connect.php'; //login to your database
$response=mysql_query("select * from questions");
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var ques, user_ans;
$('[id^=next]').click(function() {
ques = $(this).attr('id').split('t')[1];
user_ans = $(this).parent().find('input:radio:checked').attr('id');
user_ans = user_ans.split('_')[0];
user_ans = user_ans.split('o')[1];
$.ajax({
type:'POST',
url: 'ajaxCheck.php',
data:'ques=' +ques+ '&ans='+user_ans,
success: function(data){
alert(data);
ques = data.split('|')[0];
ans = data.split('|')[1];
$('#correct_'+ques).val(ans);
}
}); //END ajax
}); //END next button.click
}); //END document.ready
</script>
</head>
<body>
<form method='post' id='quiz_form' >
<?php while($result=mysql_fetch_array($response)){ ?>
<div id="question_<?php echo $result['id'];?>" class='questions'>
<h2 id="question_<?php echo $result['id'];?>">
<?php echo $result['id'].".".$result['question'];?>
</h2>
<div class='align'>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name="radio<?php echo $result['id'];?>" >
<label id='answer1_<?php echo $result['id'];?>' for='answer1_<?php echo $result['id'];?>'><?php echo $result['answer1'];?></label>
<br/>
<input type="radio" value="2" id='radio2_<?php echo $result['id'];?>' name="radio<?php echo $result['id'];?>" >
<label id='answer2_<?php echo $result['id'];?>' for='answer2_<?php echo $result['id'];?>'><?php echo $result['answer2'];?></label>
<br/>
<input type="radio" value="3" id='radio3_<?php echo $result['id'];?>' name='radio<?php echo $result['id'];?>' >
<label id='answer3_<?php echo $result['id'];?>' for='answer3_<?php echo $result['id'];?>'><?php echo $result['answer3'];?></label>
<br/>
<input type="radio" value="4" id='radio4_<?php echo $result['id'];?>' name='radio<?php echo $result['id'];?>' >
<label id='answer4_<?php echo $result['id'];?>' for='answer4_<?php echo $result['id'];?>'><?php echo $result['answer4'];?></label>
<input type="radio" checked='checked' value="5" style='display:none' id='radio4_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'>
<br/>
<input type="text" disabled="disabled" id="correct_<?php echo $result['id'];?>" />
</div>
<br/>
<input type="button" id='next<?php echo $result['id'];?>' value='Next!' name='question' class='butt'/>
</div>
<?php }?>
</form>
</body>
</html>
FILE 2: ajaxCheck.php 文件2:ajaxCheck.php
$ques = $_POST['ques'];
$ans = $_POST['ans'];
$query = "select id,answer,answer1,answer2,answer3,answer4 from questions WHERE id='$ques'";
$result=mysql_query($query) or die(mysql_error());
$r = mysql_fetch_assoc($result);
//You need to send back the ques# so jQuery knows which question block to update.
$out = $ques . '|' . $r['answer'];
echo $out;
Can you try this, added name as name='myradio[]'
您能尝试一下吗,将名称添加为name='myradio[]'
HTML: HTML:
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='myradio[]' >
PHP PHP
<?php
print_r($_POST[myradio]);
?>
since i don't know what was the condition being checked i just wrote a echo statement before the condition and then i came to know that it was checking the value of radio button and not its answer so what i did was i changed the correct answer field in the database to the correct answer's no 因为我不知道要检查的条件是什么,所以我只是在条件之前写了一个echo语句,然后我才知道它正在检查单选按钮的值,而不是其答案,所以我所做的是更改了正确答案数据库中的字段为正确答案为否
while($response=mysql_fetch_array($result))
{
echo $response['answer'];//this showed the answer
echo $_POST["$i"];//this showed the radio button no of the answer clicked
//so thats why the condition when wrong all the time and so i changed the answer filed to the right answer's radio button number and hence the condition went true
if($response['answer']==$_POST["$i"])
{
$right_answer++;
}
else if($_POST["$i"]==5)
{
$unanswered++;
}
else
{
$wrong_answer++;
}
$i++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.