I'm working on a fill-in-the-blank quiz (current state). In my index.php
, I want to verify that the user input
is the correct answer (as in, it matches what's in my MySQL server).
I can verify the answer is correct by <?php echo ... ?>
-ing the answer, but this results in the answer being in the in the html file, which has the drawback of allowing cheating.
I gather from this answer, that I need to use something like $.post('file.php', {variable: variableToSend});
, so I attempt to do that:
<script>
function process_guess() {
var input = [...snip...];
[...snip...]
var is_correct = $.post('process_answer.php', {guess: input});
[...snip...]
if(is_correct == 1) {
[...snip...]
}
else {
[...snip...]
}
}
</script>
The above Javascript function is called from a html form elsewhere in my index.php
; I can used console.log
to verify that the input
variable above is the actual user input. However, judging from console.log
, the way I'm using $.post
is totally wrong, but I'm not sure what the correct syntax is.
In process_answer.php
I have:
<?php
if($_POST['guess'] == $problem_data["answer"]) echo "1";
else echo "0";
?>
I'm not sure how to proceed from here.
Question: How do I determine if a HTML-form answer is correct (calling PHP via $.post), without revealing the answer?