eagle1771 Posted May 6, 2008 Share Posted May 6, 2008 Hello Everyone, In my spare moments I have been trying to learn Php and a little MySQL. I have a script which writes the contents of a form to a database and emails me the results, after checking the CAPTCHA form field input. The problem that I am having is that regardless of the results of the Captcha test, the script continues as if the Captcha test was a success instead of a failure. Do I need a WHILE or a FOR statement? I kind of know what the problem is but not how to write it. Here is the relevant code: <?php error_reporting(E_ALL); session_start(); $errors = array(); // set the errors array to empty, by default $fields = array(); // stores the field values $success_message = ""; $captcha_success_message = "CAPTCHA was entered correctly - Thank You"; $captcha_failure_message = "CAPTCHA was entered incorrectly - Please try again"; // more code, etc; // test for valid captcha if(isset($_POST["captcha"])) if($_SESSION["captcha"]==$_POST["captcha"]) { //CAPTCHA is valid write to database and send email; echo $captcha_success_message; } else { echo $captcha_failure_message; } // create variables from form data if ($_POST) { foreach($_POST as $fn => $v) { $v = trim($v) ; $$fn = $v ; } } // make connection to server, email results, etc.; ?> Thank you in advance for any help. Ralph Moore Link to comment https://forums.phpfreaks.com/topic/104445-php-for-or-while-question-i-think/ Share on other sites More sharing options...
Barand Posted May 6, 2008 Share Posted May 6, 2008 Use FOR when you want to do a predefined number of iterations Use WHEN when you want to loop until the value of a particular condition changes from true to false (or false to true) Link to comment https://forums.phpfreaks.com/topic/104445-php-for-or-while-question-i-think/#findComment-534669 Share on other sites More sharing options...
moselkady Posted May 6, 2008 Share Posted May 6, 2008 I think you need to exit your code when the test fails: <?php if($_SESSION["captcha"]==$_POST["captcha"]) { //CAPTCHA is valid write to database and send email; echo $captcha_success_message; } else { echo $captcha_failure_message; exit; } ?> Link to comment https://forums.phpfreaks.com/topic/104445-php-for-or-while-question-i-think/#findComment-534707 Share on other sites More sharing options...
eagle1771 Posted May 6, 2008 Author Share Posted May 6, 2008 Barand, Can you show me an example of how to use that in my code? Thanks. Ralph Moore Link to comment https://forums.phpfreaks.com/topic/104445-php-for-or-while-question-i-think/#findComment-534723 Share on other sites More sharing options...
eagle1771 Posted May 6, 2008 Author Share Posted May 6, 2008 moselkady, I tried using the "exit;" statement and it certainly does exit the script. I end up with my error statement displayed on a blank page. What I am trying to accomplish is to allow the visitor to correct their error and then continue with their form submission. That is why I thought I might need something along the lines of an IF statement. Like, IF the error exists loop until the error is corrected, and then continue with the script. Any ideas on how to do this? Thanks. Ralph Moore Link to comment https://forums.phpfreaks.com/topic/104445-php-for-or-while-question-i-think/#findComment-534728 Share on other sites More sharing options...
eagle1771 Posted May 6, 2008 Author Share Posted May 6, 2008 Can anyone tell me if the following code makes sense, or is the rest of the script going to run regardless of whether the captcha test is a success or a failure (not a match)? // more code above; // test for valid captcha; if(isset($_POST["captcha"])) //If CAPTCHA is NOT valid - display error message for additional user input; if ($_SESSION["captcha"]!==$_POST["captcha"]) { echo $captcha_failure_message; } elseif ($_SESSION["captcha"]==$_POST["captcha"]) { //CAPTCHA is valid - write to database and send email; echo $captcha_success_message; } endif // script continues; Thanks, Ralph Moore Link to comment https://forums.phpfreaks.com/topic/104445-php-for-or-while-question-i-think/#findComment-534816 Share on other sites More sharing options...
moselkady Posted May 7, 2008 Share Posted May 7, 2008 I assume that you have a form and when it is submitted you check a value and display a success or failure message. In the case of failure you also display the form again for re-submission. Here is a sample code "test.php" that you may want to modify to fit your project: <?php // test.php // function to display the form function display_form() { ?> <form action="test.php" method="post"> CAPTCHA: <input type="text" name="captcha"> <br> <input type="submit" value="send"> </form> <?php } // this part executes when captcha submitted if(isset($_POST["captcha"])) { if($_SESSION["captcha"]==$_POST["captcha"]) { //CAPTCHA is valid write to database and send email; echo $captcha_success_message; } else { echo $captcha_failure_message; display_form(); } } else display_form(); ?> Link to comment https://forums.phpfreaks.com/topic/104445-php-for-or-while-question-i-think/#findComment-534983 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.