Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/86310-solved-nested-if-statement/
Share on other sites

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.

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>"); }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.