在提交之前修改$ _POST变量

I'm making a quiz generator, and I have an HTML form with radio buttons for multiple choice answers. Right now, when I submit my form, the contents of the $_POST variable looks like this when I submit it:

Array ( [a1] => Bob [a2] => Bobby )

(Bob and Bobby are the radio button choices I picked)

I'm generating this form using a PHP function, which returns an array of answers in addition to echoing the HTML form. Is there a way to modify the $_POST variable to have an 'answer' field (for checking my answers), like this:

 Array( [a1] => Bob [a2] => Bobby [answers] => Array( [0] => Bob [1] => Bilbo ))

The above was one way I thought of to check answer array with $_POST array.

Edit: More info on what I have so far:

  • I have a PHP function getTest() that echoes the HTML form, and returns an array with the correct answers.
  • getTest() generates each question randomly, and as such the correct answers are random.
  • The main problem is that I have two separate PHP files, questions.php and verify.php.
    • questions.php echoes the form using getTest(), and has the array of answers.
    • verify.php only has the contents of $_POST, BUT NOT the array of correct answers.

Is there a better way to check the results of the form submission in general? Thanks!

The best way to do a quiz is to have an answers array and a user input array. Loop through one and compare to the other using the same increment.

You can take all of your post variables and create an array print_r($_POST); Then, loop through this.

$inputArray = //the post data into an array
$answerArray = array('a','b','a');
$numCorrect = 0;
for($a = 0; $a < count($inputArray); $a++)
{
if($inputArray[$a] == $answerArray[$a])
{
$numCorrect++;
}
}

If you want $_POST to contain an array you can simply use the bracket array notation on the name field of your form.

For example:

<fieldset class="Question1"> 
  <input type="radio" name="answers[]" value="Question1Answer1">Question1Answer1<br>
  <input type="radio" name="answers[]" value="Question1Answer2">Question1Answer2<br>
  <input type="radio" name="answers[]" value="Question1Answer3">Question1Answer3<br>
</fieldset>

<fieldset class="Question2"> 
  <input type="radio" name="answers[]" value="Question2Answer1">Question2Answer1<br>
  <input type="radio" name="answers[]" value="Question2Answer2">Question2Answer2<br>
  <input type="radio" name="answers[]" value="Question2Answer3">Question2Answer3<br>
</fieldset>

<fieldset class="Question3"> 
  <input type="radio" name="answers[]" value="Question3Answer1">Question1Answer1<br>
  <input type="radio" name="answers[]" value="Question3Answer2">Question1Answer2<br>
  <input type="radio" name="answers[]" value="Question3Answer3">Question1Answer3<br>
</fieldset>

(Note that the fieldset tag is optional, I just included it to group things together)

The output in post will be an array $_POST['answers'] that will have one element for each question. So if you selected answer 1 for question 1, answer 2 for question 2, and answer 2 for question 3 you'd have:

$_POST['answers'] = [ 'Question1Answer1', 'Question2Answer2', 'Question3Answer2' ]

If you want to transmit the answers when submitting the form, you could use inputs of hidden type (like ) which are invisible on the page. However it only takes the user checking the source HTML of the page to see these answers, so it might not be good for your use. Hope this helps

I think what you need to do is a have a look at sessions.

  • That way on questions.php you can save the answers to a session variable,
  • Then on verify.php you can read the answers from the session variable and compare them to answered supplied by the $_POST variable

If you really wanted to, you could probably just use a hidden field in your form for submitting an answer array. However, anyone can change your source and modify what the correct answer is.

The best way is to just have an array in your processing script with the same keys (a1, a2), but with the correct answers.

Your processing script would look like this:

$answers = array('a1'=>'Robert', 'a2'=>'Hobo');

foreach($_POST as $key => $value)
{
   if (!array_key_exists($key, $answers))
   {
       continue;
   }

   if (trim($value) == $answers[$key])
   {
        // correct
   }
   else
   {
        // incorrect
   }
}

Not sure, but looks like you are asking for solution Y, whereas your problem is X (XY Problem)

The XY problem is when you need to do X, and you think you can use Y to do X, so you ask about how to do Y, when what you really should do is state what your X problem is. There may be a Z solution that is even better than Y, but nobody can suggest it if X is never mentioned.

Usually it is not recommended to modify $_POST array, and also not to transmit Answers with the questions to client-side. Instead, the approach should be that because questions.php dont need answers, but verify.php does, so only verify.php shoul have access to answers.

For example, answer-lists are never transported to examination halls along with the question papers on the occasion of exams.

I have taken the liberty to modify your code structure. If you still want to go with your own code, please post it, and then you can get the answers you want.

Try to use this:

question.php:

<form action="verify.php" method="POST">
<fieldset class="Question1"> Complete this: ___<b>bar</b>
  <input type="radio" name="answers[]" value="foo">Foo<br>
  <input type="radio" name="answers[]" value="too">Too<br>
  <input type="radio" name="answers[]" value="cho">Cho<br>
</fieldset>

<fieldset class="Question2"> Complete this: ___<b>overflow</b> 
  <input type="radio" name="answers[]" value="stack">Stack<br>
  <input type="radio" name="answers[]" value="stock">Stock<br>
  <input type="radio" name="answers[]" value="stick">Stick<br>
</fieldset>
</form>

answers.php:

//correct answers
$answers = array("foo", "stock");

verify.php:

include("answers.php");
$user_answers = $_POST["answers"];
$user_answers_count = count($user_answers);

$error = "";
for($i=0;$i<$user_answers_count;$i++)
    if($user_answers[$i] !== $answers[$i]) //verify
        $error[] = "Answer for Question ".($i+1)." is wrong!";

if(empty($error))
    //Notify that user has passed the Quiz
else
    //Notify that user has NOT passed the Quiz
    //print the $error array 

Couple of Notes:

  • I have used answers.php as a different file, but if there is no special requirement, consider merging answers.php & verify.php (put answers.php code on top of verify.php) Even better, you could also merge all these three files into one.
  • I have assumed that $_POST is sanitized.
  • Sequence of Questions & answers array is same. i.e. answers[foo] is correct answer for $_POST["answers"][foo]