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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛