PHP检查是否单击了单选按钮

时间:2014-01-17 05:54:55

标签: php mysql

我想知道用户是否点击了提交按钮,它会检查用户是否检查了所有的无线电按钮。

这是我到目前为止所尝试的:

  $stmt = $conn->prepare( "SELECT *
      FROM tblquiz ORDER BY rand()" );
      $stmt->execute();

  ?>
  <form method="post" action="checkquiz.php">
    <?php 
    $number = 0;
    for($i=0; $row = $stmt->fetch(); $i++){
            $number++;  
            $id = $row['_id'];
            $question = $row['question'];
            $answers = array($row['answer1'],$row['answer2'],$row['answer3'],$row['correctanswer']);
            shuffle($answers);
    ?>

     <h4> <?php echo $number . ".) " . $question; ?></h4>   
     <label><input type="radio" value="<?php echo $answers[0]; ?>" name="<?php echo $question; ?>"> <?php echo $answers[0]; ?></label>
     <label><input type="radio" value="<?php echo $answers[1]; ?>" name="<?php echo $question; ?>"> <?php echo $answers[1]; ?></label>
     <label><input type="radio" value="<?php echo $answers[2]; ?>" name="<?php echo $question; ?>"> <?php echo $answers[2]; ?></label>
     <label><input type="radio" value="<?php echo $answers[3]; ?>" name="<?php echo $question; ?>"> <?php echo $answers[3]; ?></label>

    <?php
    }
    ?>
    <br />
    <br />
    <input type="submit" value="Submit" name="submit">
</form>

CheckQuiz.php

<?php 


if( isset($_POST['submit'])) {
    //echo 'You clicked submit!';

    if(  ){

    } else {

    }


} else {

    echo "You didn't click submit!";

}


?>

我是新手,所以我真的需要你的帮助。如果检查了所有单选按钮,我首先要检查,然后检查用户的答案。你能给我一些这样的实例吗?非常感谢您的帮助谢谢。

2 个答案:

答案 0 :(得分:3)

首先,您具有相同名称的所有单选按钮的名称。说它的名字是question。以下示例

<input type="radio" name="question" value="1">
<input type="radio" name="question" value="2">
<input type="radio" name="question" value="3">
<input type="radio" name="question" value="4">

然后您可以从服务器访问其值

if(isset($_POST["question"])){   
   echo $_POST["question"]; 
}else{
  echo "No answers were selected";
}

按用户需要编辑

如果您有多个问题,那么您可以使用如下

Question 1:

<input type="radio" name="question[1]" value="1">
<input type="radio" name="question[1]" value="2">
<input type="radio" name="question[1]" value="3">
<input type="radio" name="question[1]" value="4">

Question 2:

<input type="radio" name="question[2]" value="1">
<input type="radio" name="question[2]" value="2">
<input type="radio" name="question[2]" value="3">
<input type="radio" name="question[2]" value="4">

然后你可以迭代如下:

   $quest = $_POST['question'];
   foreach($quest as $key=>$val){
         echo "Question ".$key." answer is ".$val."<br/>";
   }

答案 1 :(得分:2)

if(isset($_POST["radioBoxName"])){
   // It was checked
   echo $_POST["radioBoxName"]; // This is the value of the checked option
}

根据评论进行修改

如果您可以说出10个问题,每个问题有4个不同的答案,那么您可以像这样命名您的电台盒

<input type="radio" name="answer[1][1]"> Question 1 Answer 1
<input type="radio" name="answer[1][2]"> Question 1 Answer 2
<input type="radio" name="answer[1][3]"> Question 1 Answer 3
<input type="radio" name="answer[1][4]"> Question 1 Answer 4

<input type="radio" name="answer[2][1]"> Question 2 Answer 1
<input type="radio" name="answer[2][2]"> Question 2 Answer 2
<input type="radio" name="answer[2][3]"> Question 2 Answer 3
<input type="radio" name="answer[2][4]"> Question 2 Answer 4

然后你可以在PHP中检查它们

// For 10 questions
for($i=1;$i<=10;$i++)
{
     if(!isset($_POST["answer"][$i]))
     echo "You didn't answer Question number $i ";
}