1

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.

4
  • You want to check whether user checked an answer right?
    – Nouphal.M
    Commented Jan 17, 2014 at 5:56
  • @Nouphal.M Yes that's exactly what I want
    – Dunkey
    Commented Jan 17, 2014 at 5:58
  • This means you want to check if any of the question is unanswered then user cannot click submit? Commented Jan 17, 2014 at 5:58
  • @user2936213 the user can still click the submit button, but in the next page I have to notify them that they have unanswered questions
    – Dunkey
    Commented Jan 17, 2014 at 6:00

2 Answers 2

3

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/>";
   }
13
  • I tried doing this, but it's only getting the last value of the 10 radio buttons,.
    – Dunkey
    Commented Jan 17, 2014 at 6:15
  • Try: name="question[]"
    – cssyphus
    Commented Jan 17, 2014 at 6:21
  • Yes I changed to question[] like this: <label><input type="radio" value="<?php echo $answers[0]; ?>" name="question[]"> <?php echo $answers[0]; ?></label> <label><input type="radio" value="<?php echo $answers[1]; ?>" name="question[]"> <?php echo $answers[1]; ?></label> <label><input type="radio" value="<?php echo $answers[2]; ?>" name="question[]"> <?php echo $answers[2]; ?></label> <label><input type="radio" value="<?php echo $answers[3]; ?>" name="question[]"> <?php echo $answers[3]; ?></label> but I can't select other answers
    – Dunkey
    Commented Jan 17, 2014 at 6:30
  • 1
    This last edit is Flawed. Naming all the boxes like name="question[]" will then force the system to have only one answer for each question, which is not desired. One question can have 4 possible options. This way it will tell php that there are 40 question with one option each. Commented Jan 17, 2014 at 6:35
  • I apologize for the mistake.
    – Nouphal.M
    Commented Jan 17, 2014 at 6:40
2
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 ";
}
14
  • What if I have 10 radio buttons?
    – Dunkey
    Commented Jan 17, 2014 at 5:59
  • Actually first I didn't understood how he will check all the radio boxes at first place as they are radios and not checkboxes, secondly, if is better to use a multi dimensional array, so that he doesn't have to write isset() for each checkbox group
    – Mr. Alien
    Commented Jan 17, 2014 at 5:59
  • 1
    10 Radio buttons with one name? or different names? Commented Jan 17, 2014 at 6:00
  • @Hanky웃Panky in every question I have 4 radiobuttons
    – Dunkey
    Commented Jan 17, 2014 at 6:02
  • @Dunkey please read what Hanky웃Panky is asking. Are your radio buttons of same name or different? Commented Jan 17, 2014 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.