ahazin Posted January 16, 2008 Share Posted January 16, 2008 hi guys pretty simple question i was wondering if you could help me with it i have the following code: if(!eregi("[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}",$email_field)) print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">Invalid E-Mail address please correct the error and try again.</strong></font>"); else if($sent) {print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">Your message was sent successfully</strong></font>"); } else {print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">We encountered an error sending your message. Please try again or call us on 07802 414985</strong></font>"); } what i was trying to acheive was that if the email is not valid it says "E-Mail address please correct the error and try again." If it is valid and it is sent it says "your message was sent successfully" and if it is valid but it is not sent it says We encountered an error sending your message. Please try again or call us on 07802 414985. At the minute if the email is invalid the message comes up stating so as expected, however if it is valid it says "We encountered an error sending your message. Please try again or call us on 07802 414985" reguardless of wether it was sent or not. Is there something stupid i am forgetting to do. Any help greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/86310-solved-nested-if-statement/ Share on other sites More sharing options...
revraz Posted January 16, 2008 Share Posted January 16, 2008 Your order of logic is not correct. It appears you are setting $sent before the IF statement, so you are sending before you are validating the email address. Validate email, if valid then process the mail() function, then check to see if it was sent. Quote Link to comment https://forums.phpfreaks.com/topic/86310-solved-nested-if-statement/#findComment-440948 Share on other sites More sharing options...
timmah1 Posted January 16, 2008 Share Posted January 16, 2008 Maybe I'm wrong, but I always thought you ran the elseif statement last if(!eregi("[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}",$email_field)) print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">Invalid E-Mail address please correct the error and try again.</strong></font>"); else {print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">We encountered an error sending your message. Please try again or call us on 07802 414985</strong></font>"); } elseif($sent) {print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">Your message was sent successfully</strong></font>"); } Quote Link to comment https://forums.phpfreaks.com/topic/86310-solved-nested-if-statement/#findComment-440952 Share on other sites More sharing options...
revraz Posted January 16, 2008 Share Posted January 16, 2008 else should always be last. You want to start with IF, if that isn't true, you want to elseif, and if that fails, it's else. Maybe I'm wrong, but I always thought you ran the elseif statement last Quote Link to comment https://forums.phpfreaks.com/topic/86310-solved-nested-if-statement/#findComment-440956 Share on other sites More sharing options...
ahazin Posted January 16, 2008 Author Share Posted January 16, 2008 ya my logic was wrong. Another stupid mistake on my behalf cheers revraz. Got it now Quote Link to comment https://forums.phpfreaks.com/topic/86310-solved-nested-if-statement/#findComment-441009 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.