dqdmvg7332 2014-01-17 05:54
浏览 103
已采纳

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

I want to know if the user clicks the submit button, it will check if the user checks all radiobuttons.

This is what I've tried so far:

  $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!";

}


?>

Im a newbie so I really need your help. I want check first if all radio buttons have been checked and then check if the user's answers. Can you give me sample implementations of this? Your help will truly be appreciated Thanks.

  • 写回答

2条回答 默认 最新

  • douduiti3040 2014-01-17 06:07
    关注

    First you have name all your radio buttons with the same name. Say its name is question. example below

    <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">
    

    Then you can access its value from server as

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

    edited as per needed by user

    if you have multiple questions then you could use like below

    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">
    

    Then you could iterate as below:

       $quest = $_POST['question'];
       foreach($quest as $key=>$val){
             echo "Question ".$key." answer is ".$val."<br/>";
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件