Dunkey
Dunkey

Reputation: 1922

PHP check if a radio button is clicked

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.

Upvotes: 1

Views: 4397

Answers (2)

Nouphal.M
Nouphal.M

Reputation: 6344

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/>";
   }

Upvotes: 3

Hanky Panky
Hanky Panky

Reputation: 46900

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

Edit based on comments

If you have lets say 10 questions with 4 different answers for each you can then name your radio boxes like this

<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

Then you can check them in PHP like

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

Upvotes: 2

Related Questions