Reputation: 787
So right now what I am trying is to randomly get a question from a set of questions in a table on the database and display them on the form for the person to answer. if that is a bad thing to do please let me know how I should go about it instead. I guess it leaves an easy way to abuse the database, perhaps? I'm not sure.
Well, right now I have the form and the form processing on the same page. The problem is that when it is submitted, it of course refreshes and changes the question, which means unless they get lucky, the answer is wrong. I am not sure how to go about setting it up so that I can generate the random question and make sure when they submit the form that it works out correctly without changing the answer. i have form validation with javascript set up, but that won't help against bots.
So basically, what I have is
check stuff, connect to databse, etc
$random = mysql_fetch_row(mysql_query("select * from questions order by rand() limit 1"));
clean submitted data, check, submit, etc.
In the form, I have
General form stuff, name, email, etc
<label for="question"><?php echo $random_row[1]; ?></label>
<input type="text" name="question" id="question" required="required"/>
So you can see what I am wanting to do, if that wasn't clear before.
Upvotes: 0
Views: 151
Reputation: 14329
If you really want to handle the input and the submission with the same page, you're going to end up with a structure like this.
if (!empty($_POST))
{
// there is $_POST data, so there must be a submission
}
else
{
// there is no $_POST data, so the user needs to input
}
To know which question the user is answering in the submission, add a hidden input to the form.
<form ...>
<input type="hidden" name="question_id" value="<?php echo $question_id; ?>" />
<input type="text" name="answer" />
</form>
When receiving the submission, you can use the question_id to lookup the correct answer in the database. Compare the correct answer to the answer the user gave.
Please ask more specifically if you require detail about a particular part.
Upvotes: 1
Reputation: 4860
Your effort seems perfect.
You need to store question id in session because when you submiting form data in next page you require question which was asked.
So you will get question detail by question id store in session and compare answer on submitted page.
Hope this may help.
Upvotes: 1