简体   繁体   English

如果未选择单选按钮,则返回错误-PHP

[英]Returning Error If Radio Button NOT selected - PHP

I've searched through a lot of previously asked questions about php and radio buttons but I haven't yet found something helping in my situation. 我已经搜索了很多先前询问的有关php和单选按钮的问题,但还没有找到对我有帮助的东西。 I'm building a quiz for a server-side scripting class. 我正在为服务器端脚本课程构建测验。 I'm stuck on the actual quiz part. 我被困在实际的测验部分。 I want an error message to echo if the user does NOT select an answer to a question. 如果用户未选择问题的答案,我希望显示一条错误消息。 Instead when I hit the submit button without selecting any options, I'm automatically redirected to the results page. 相反,当我在不选择任何选项的情况下点击了“提交”按钮时,我会自动重定向到结果页面。 How do I correct my code so that the user is given the error message if they hit submit without answering all of the questions? 我该如何纠正我的代码,以便如果用户在未回答所有问题的情况下点击“提交”,则会收到错误消息? Below is the current code i've written out in an attempt to check the radio buttons and next steps. 以下是我为检查单选按钮和后续步骤而写出的当前代码。

<div id="question5"> <!-- each quiz question gets this set up -->
<h2 id="question5_error">WAIT! You did not pick an answer to this question!</h2>
<p>5. What creature pulls the Hogwarts carriages?<br>
<input type="radio" name="creature" value="Cornish Pixies" <?php if (isset($_SESSION["creature"]) && $_SESSION["creature"]="Cornish Pixies"){ echo"checked";} ?> > Cornish Pixies<br> <!-- php code checks to see if radio button has been selected-->
<input type="radio" name="creature" value="Clydesdales" <?php if (isset($_SESSION["creature"]) && $_SESSION["creature"]="Clydesdales"){ echo"checked";} ?> > Clydesdales<br>
<input type="radio" name="creature" value="Hippogriffs" <?php if (isset($_SESSION["creature"]) && $_SESSION["creature"]="Hippogriffs"){ echo"checked";} ?> > Hippogriffs<br>
<input type="radio" name="creature" value="Thestrals" <?php if (isset($_SESSION["creature"]) && $_SESSION["creature"]="Thestrals"){ echo"checked";} ?> > Thestrals<br>

<input type="hidden" name="sent" value="sent" onclick="Project1_Page2.php">
<input type="submit" name="goresults" value="Get Results?">|<input type="reset" name="resetquiz" value="Reset Quiz.">
</form> 
</article>
<?php
if(isset($_GET["sent"])){
echo "<script type = 'javascript'>";
if(!isset($_GET["question1"])){ // QUIZ QUESTION 1 CHECK
       $_SESSION["author"]=$_GET["author"];
    }
    else{
        echo "document.getElementById('question1').style.backgroundColor = '#ffcccc';
        document.getElementById('question1_error').style.display = 'block';";
    }
if(!isset($_GET["question2"])){ // QUIZ QUESTION 2 CHECK
    $_SESSION["godfather"]=$_GET["godfather"];
    }
    else{
        echo"document.getElementById('question2').style.backgroundColor = '#ffcccc';
        document.getElementById('question2_error').style.display = 'block';";
    }
if(!isset($_GET["question3"])){ // QUIZ QUESTION 3 CHECK
    $_SESSION["prison"]=$_GET["prison"];
    }
     else
    {
        echo"document.getElementById('question3').style.backgroundColor = '#ffcccc';
        document.getElementById('question3_error').style.display = 'block';";
    }   
if(!isset($_GET["question4"])){ // QUIZ QUESTION 4 CHECK
    $_SESSION["badguy"]=$_GET["badguy"];
    }
    else
    {
        echo"document.getElementById('question4').style.backgroundColor = '#ffcccc';
        document.getElementById('question4_error').style.display = 'block';";
    }
if(!isset($_GET["question5"])){ // QUIZ QUESTION 5 CHECK
    $_SESSION["creature"]=$_GET["creature"];
    }
    else
    {
        echo"document.getElementById('question5').style.backgroundColor = '#ffcccc';
        document.getElementById('question5_error').style.display = 'block';";
    }
echo "</script>";
}
?>

First of all if you are using radios you have set their attribute name to one like 首先,如果您使用收音机,则将其属性名称设置为类似

<form method="post">
    <input name="question" value="1" />
    <input name="question" value="2" />
    <input name="question" value="3" />
    <input name="question" value="4" />
</form>

in this way you set your logic statement like so. 这样,您可以像这样设置逻辑语句。


    if(isset($_POST['question'])){

    //here you put the the right answer based on the user what he/she chose.


    }

I suggest you to use POST is secured. 我建议您使用POST是安全的。 If you are using GET the values of radio will appear on the url. 如果您使用的是GET,则radio的值将显示在url上。 I hope this will help. 我希望这将有所帮助。

Peace 和平

Without seeing your HTML, it's hard to tell but here are some suggestions 没有看到您的HTML,很难分辨,但是这里有一些建议

in your form tag, remove "Action" attribute 在您的表单标签中,删除“操作”属性

<form method="post/get" >

then create a variable 然后创建一个变量

$haserror = false;

and I'm not sure what are you trying to achieve here, but change the code like this 我不确定您要在这里实现什么,但是请像这样更改代码

if(!isset($_GET["question1"])){ // QUIZ QUESTION 1 CHECK
       $_SESSION["author"]=$_GET["author"];
        echo "document.getElementById('question1').style.backgroundColor = '#ffcccc';
        document.getElementById('question1_error').style.display = 'block';";

$haserror = true; $ haserror = true; } }

do the same for all checkboxes 对所有复选框执行相同的操作

then at the end do your if to show the result page 然后在最后执行是否显示结果页面

if(!$haserror)
{
    echo '<META http-equiv="refresh" content="0;URL=newpage.php">';

}

however I suggest to rewrite the whole thing, because you are approaching it in a wrong way 但是我建议重写整个内容,因为您以错误的方式处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM