提交页面后,如何检查单选按钮

时间:2017-03-23 06:01:08

标签: php sql

问题

**question_id  question  option1 option2 option3 option4   answer** 
15          AAA         a1     a2      a3       a4           3
38          BBB         b1     b2      b3       b             2
39          CCC         c1     c2      c3       c4            4
44          DDD         d1     d2      d3       d4            2
45          EEE         e1     e2      e3       e4            1

start_exam.php

$query="select * from exam_question_list where exam_paper_id='$exam_paper_id'";
$result=mysqli_query($dbcon,$query);
$row = mysqli_fetch_array($result);
$que1=$row['q1'];

<form action="exam_paper_result.php?1=<?php echo $row['q1'];?>>
<?php 
    $sql1="SELECT question_id AS qid, question AS q, option1 AS op1, option2 AS op2, option3 AS op3, option4 AS op4 FROM question where question_id='$que1'";


        $data1=mysqli_query($dbcon,$sql1);

    while($datan1 = mysqli_fetch_array($data1)){

        $i=0;
?>
  <tr>
     <tr data-label="Question"><td><span>(01)&nbsp;&nbsp;</span><?php echo $datan1['q'];  ?></td></tr>
      <tr data-label="Question"><td><input type="radio"  name="a1" value="1"/><?php echo $datan1['op1']; ?></td></tr>
      <tr data-label="Question"><td><input type="radio"  name="a1" value="2"/><?php echo $datan1['op2']; ?></td></tr>
      <tr data-label="Question"><td><input type="radio"  name="a1" value="3"/><?php echo $datan1['op3']; ?></td></tr>
      <tr data-label="Question"><td><input type="radio"  name="a1" value="4"/><?php echo $datan1['op4']; ?></td></tr>  

    </tr>


<?php
    }
    ?>

 <input name="submit" type="submit" id="Submit" value="Submit"/>
</form>

exam_paper_result.php

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


$qu1 =$_GET['1'];
$q1 =  $_POST['a1'];

$score ="";

$sql1="SELECT question_id AS qid, answer AS a FROM question where question_id='$qu1'";
$result1=mysqli_query($dbcon,$sql1);
$row1=mysqli_fetch_array($result1);

if (!isset($q1)) {
 echo("Error: please specify a type, new or old");
 //$score = 0;

 } else {

if($row1['a'] == $q1){
    echo "correct";
    //$score = 1;

}else{
    echo "wrong";
    //$score = 1;
}   
}

 }
  ?>

当我选择正确的选项然后打印'正确' 当我选择错误的选项然后打印'错误' 当我没有选择任何选项并单击提交时;有错误

“注意:未定义的索引:第38行的G:\ xampp \ htdocs \ examination_system \ src \ applicant \ exam_paper_result.php中的a1 错误:不选择任何答案“

2 个答案:

答案 0 :(得分:0)

如果没有选中单选按钮,则可能会在此行中显示该通知:

$q1 =  $_POST['a1'];

将其更改为:

 $q1 =  isset($_POST['a1']) ? $_POST['a1'] : NULL; 

答案 1 :(得分:0)

确定变量是否已设置且不为NULL。所以你需要使用isset函数。

 $q1 = null;    
 if(isset($_POST['a1'])){
 $q1 = $_POST['a1'];
}

你也可以使用这个条件

$q1 = isset($_POST['a1']) ? $_POST['a1'] : null;

并且您需要这样的任何收音机类型,然后您不能在不检查任何收音机的情况下提交此表格

<form>
<label for="input1">Label 1:</label>
<input type="radio" name="radio" id="input1" value="1" required>     <br>

<label for="input2">Label 2:</label>
 <input type="radio" name="radio" id="input2" value="2"><br>

<label for="input3">Label 3:</label>
<input type="radio" name="radio" id="input3" value="3"><br>

<input type="submit" value="send">
</form>

有关详细信息,请查看此

http://php.net/manual/en/function.isset.php