hanlonj Posted June 22, 2007 Share Posted June 22, 2007 Hi, I have an html form and a lot of form validation in my processing script. I use many "if" statements before I get to the part of the script where the form contents are emailed to a company. Here is my question, my script validates all fields in the form but if it finds an error it notifies the user to try again, however, the program continues to execute down to the "mail()" function no matter what errors are on the form. I have tried to use "break;" at the end of each "if" statement but it won't work. Any suggestions on how to stop the program if my error checking traps an error? stoney Quote Link to comment https://forums.phpfreaks.com/topic/56781-solved-break-an-if-statement/ Share on other sites More sharing options...
simcityfreak4 Posted June 22, 2007 Share Posted June 22, 2007 Maybe die(); ? Quote Link to comment https://forums.phpfreaks.com/topic/56781-solved-break-an-if-statement/#findComment-280437 Share on other sites More sharing options...
SycoSyco Posted June 22, 2007 Share Posted June 22, 2007 exit might be what you're looking for http://us2.php.net/manual/en/function.exit.php Quote Link to comment https://forums.phpfreaks.com/topic/56781-solved-break-an-if-statement/#findComment-280438 Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 show your code and the issue location this could be 1 of 50 things Quote Link to comment https://forums.phpfreaks.com/topic/56781-solved-break-an-if-statement/#findComment-280439 Share on other sites More sharing options...
marcus Posted June 22, 2007 Share Posted June 22, 2007 die(); would work easily, but if you put it all together and leave the mail function at the end it would work: <?php $var = $_POST['var']; if($var){ if(strlen($var) > 32){ echo "error"; }else { if(!ctype_alnum($var)){ echo "error"; }else { //send mail } } }else { echo "var not defined"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56781-solved-break-an-if-statement/#findComment-280442 Share on other sites More sharing options...
pocobueno1388 Posted June 22, 2007 Share Posted June 22, 2007 Do something like this: <?php if (empty($var) || $var <2){ //This is just an example of your error checking echo "Error checking found something wrong, please fix the error"; } else { //put your mail script here } ?> The key to that code is the ELSE statment, if it finds errors it won't execute the mailing part. Your other option would be exiting the rest of the script if the statement did find errors. To do that, you would just put this at the end of your if statement that checks for errors. exit; Although that will exit everything below that....so if you have footers, they will not be shown. so the first way might be the better method in your case. Quote Link to comment https://forums.phpfreaks.com/topic/56781-solved-break-an-if-statement/#findComment-280443 Share on other sites More sharing options...
hanlonj Posted June 22, 2007 Author Share Posted June 22, 2007 Excellent as per Usual you php freaks! Thanks a mill. Solved. Stoney Quote Link to comment https://forums.phpfreaks.com/topic/56781-solved-break-an-if-statement/#findComment-280474 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.