mostafatalebi Posted January 1, 2013 Share Posted January 1, 2013 (edited) Hello everybody I have a problem in my form submission which I think could be easily solved by a pro programmer Setting: I have a form At the end of the form I have put something like a CAPTCHA. There is a question, and a text input for the user to enter the answer. So far everything works fine, the question is randomly generated without any problem. But as soon as the user clicks on the submit button, the script regenerates the question which naturally has a different answer than the one the user has entered. What should I do to avoid execution of the function when the form is submitted?? However if somebody likes to see the script architecture here it is: function randomSentenceGenerator () { php script } $question_and_answer_array = randomSentenceGenerator(); if(isset($_POST['submit']) { if(question != answer) { tell user to enter a correct answer } Include the HTML } else { include the HTML } Edited January 1, 2013 by mostafatalebi Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 You'll need to store the correct answer in the session and use that one to check your form. Quote Link to comment Share on other sites More sharing options...
mostafatalebi Posted January 1, 2013 Author Share Posted January 1, 2013 I have done this. But the problem is when the user submit the form the session updates to the new value Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 You have a logic problem then. You need to see if the form is submitted before replacing the session value. Quote Link to comment Share on other sites More sharing options...
mostafatalebi Posted January 1, 2013 Author Share Posted January 1, 2013 logic is the same as you expect, but still doesn't Quote Link to comment Share on other sites More sharing options...
kicken Posted January 1, 2013 Share Posted January 1, 2013 logic is the same as you expect, but still doesn't Your logic is not as one would expect, it is incorrect. Using your above template as an example, the correct logic flow would be something like: function randomSentenceGenerator () { php script } if(isset($_POST['submit']) { if(question != answer) { tell user to enter a correct answer } Include the HTML } else { $question_and_answer_array = randomSentenceGenerator(); include the HTML } You generate a question only if the form was not submitted (or, optionally, if they answer incorrectly). When the form has been submitted you compare the post'ed answer with the answer in the session. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.