检查正确答案并提交表格 [英] Checking Correct Answer and Submitting Form
问题描述
我正在尝试创建一个非常简单的测验,其中包含正确答案的数量(例如,domaindotcom /?如果他们到目前为止有3个正确答案,那么p = 3)。
我使用下面的PHP代码来启动它:
<?php
/ *获取当前正确答案Count * /
$ answer_count = $ _GET [p];
*检查提交的答案是否与正确答案相同* /
if($ _POST [submitted-answer] ==correct-answer){
$ answer_count ++;
}
?>
现在我知道我可以通过链接获得正确的链接:
< a href =link-to-next-question.php /?p =<?php echo $ answer_count;?>>下一个问题< / a>
但现在我试图在表单中使用它,并被POST,GET等混淆。
这是我的HTML:
< form name =quiz action =method =POST>
< label for =o1>< input type =radioname =grpid =o1value =o1>标签1< / label>
< label for =o2>< input type =radioname =grpid =o2value =o2>标签2< / label>
< label for =o3>< input type =radioname =grpid =o3value =o3>标签3< / label>
< input type =submitvalue =Next Questionclass =btn>
< / form>
如何选择正确的答案(安全性不重要,只是一个有趣的测验),然后发送他们一直到下一个URL,同时在创建URL之前将增量添加到$ answer_count?
不要通过在链接中回答计数(即通过GET)。相反,只需包含一个隐藏的表单字段,并使用客户端JavaScript代码来递增所需的变量并提交表单即可。
< form id =qfrmname =quizaction =method =POST>
< input type =hiddenname =question_numbervalue =<?php echo $ next_question_number?>>
< input type =hiddenid =n_c_aname =num_correct_answersvalue =<?php echo $ num_correct_answers?>>
< input type =buttonvalue =提交onclick =validateAnswerAndSubmit();返回false;>
< / form>
< script language =javascript>
function validateAnswerAndSubmit(){
if(validate_answer )){
document.getElementById(n_c_a)。value + = 1;
}
document.getElementById(qfrm)。submit();
}
$ lt; / script>
然后让你的php脚本开启 $ _POST [question_number]
好吧,你不想使用javascript ...如果你真的问我怎么知道从PHP中选择了哪个单选框?答案是:
pre $ code ><?php $ answer = $ _POST [grp];?>
我认为你应该在URL中传递两个变量,一个是question_number,另一个是num_correct,然后你可以这样写代码:
<?php
$ num_correct =(int)$ _GET [num_correct];
$ question_number =(int)$ _GET [question_number];
switch($ question_number - 1){//假设您的问题编号按照惯例排序
//验证上一个问题的答案
case -1://第一个问题不需要验证($ question_number 0)
break;
case 0:
if($ _ POST [grp] ==correct answer){
$ num_correct ++;
}
break;
//等等;
}
?>
< form name =quiz
action =this_page.php /?num_correct =<?php echo $ num_correct;?>& question_number =<?php echo $ question_number + 1?>
method =POST>
<?php Display_Question_Number($ question_number);?>
< / form>
这里的关键在于表单中的action =类似于href =in锚,即它是当用户点击提交按钮时提交表单的URL。
I am admittedly not as skilled in PHP as I would hope to be. Most of my experience is with Wordpress loops.
I'm trying to create a very simple quiz that carries the number of correct answers in the URL (eg. domaindotcom/?p=3 if they've got 3 correct answers so far).
I'm using the following PHP code to start it off:
<?php
/* Gets current correct answer Count */
$answer_count = $_GET["p"];
/* checks to see if the submitted answer is the same as the correct answer */
if ($_POST["submitted-answer"] == "correct-answer") {
$answer_count++;
}
?>
Now I know I can get the correct link by using the link:
<a href="link-to-next-question.php/?p=<?php echo $answer_count; ?>">Next Question</a>
But now I'm trying to use it in a form and confused by POST, GET, etc.
Here's my HTML:
<form name="quiz" action="" method="POST">
<label for="o1"><input type="radio" name="grp" id="o1" value="o1"> Label 1</label>
<label for="o2"><input type="radio" name="grp" id="o2" value="o2"> Label 2</label>
<label for="o3"><input type="radio" name="grp" id="o3" value="o3"> Label 3</label>
<input type="submit" value="Next Question" class="btn">
</form>
How can I choose the correct answer (security not important, it's just a fun quiz) and then send them along to the next URL while adding the increment to the $answer_count before the URL is created?
Don't pass the answer count in the link (i.e. by GET). Instead just include a hidden form field(s), and use client side javascript code to increment variables you want and submit the form.
<form id="qfrm" name="quiz" action="" method="POST">
<input type="hidden" name="question_number" value="<?php echo $next_question_number?>">
<input type="hidden" id="n_c_a" name="num_correct_answers" value=<?php echo $num_correct_answers?>">
<input type="button" value="Submit" onclick="validateAnswerAndSubmit(); return false;">
</form>
<script language="javascript">
function validateAnswerAndSubmit(){
if(validate_answer()){
document.getElementById("n_c_a").value += 1;
}
document.getElementById("qfrm").submit();
}
</script>
Then just make your php script switch on $_POST["question_number"]
OK so you don't want to use javascript... if you're really asking "how do I know which radio box was selected from PHP?" the answer is:
<?php $answer = $_POST["grp"]; ?>
I think you should really be passing two variables in the URL, one would be the question_number, and the other would be num_correct. You could then write code like this:
<?php
$num_correct = (int) $_GET["num_correct"];
$question_number = (int) $_GET["question_number"];
switch($question_number - 1){ // assuming your question numbers are in order by convention
// validate the answer to the previous question
case -1: //no validation necessary for the first question ($question_number 0)
break;
case 0:
if($_POST["grp"] == "correct answer"){
$num_correct++;
}
break;
// and so forth;
}
?>
<form name="quiz"
action="this_page.php/?num_correct=<?php echo $num_correct;?>&question_number=<?php echo $question_number + 1?>"
method="POST">
<?php Display_Question_Number($question_number);?>
</form>
The key point here is that "action=" in the form is analogous to "href=" in the anchor, i.e. it's the URL to which the form is submitted when the user clicks the submit button.
这篇关于检查正确答案并提交表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!