开发者

Email validation, using if else statements wont allow it to continue checking if there was an error with the first if?

开发者 https://www.devze.com 2023-01-30 18:15 出处:网络
I have: if(isset($_POST[\'submit\'])) { if (empty($name)) { echo\'<span class=\"error\">ERROR: Missing Name </span><br/>\';

I have:

if(isset($_POST['submit'])) {
    if (empty($name)) {
        echo'<span class="error">ERROR: Missing Name </span><br/>';
    } else if(empty($phone) || empty($email)) {
        echo'<span class="error">ERROR: You must insert a phone number or email</span><br/>';
    } else if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        echo'<span class="error">ERROR: Please Insert a valid Email开发者_运维技巧</span><br/>';
    }  else {
        mail( "anEmail@hotmail.com", "Monthly Specials Email",
            "Name: $name 
            Email: $email 
            Phone Number: $phone
            Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}

How else could I check for all of those issues without using else if? When I use else if, it checks through the first if statement, if there is an issue with it it will not continue going through the other if statements following that one.

Any ideas? Thank you


You could collect all errors in an array like this:

if (isset($_POST['submit'])) {
    $errors = array();
    if (empty($name)) {
        $errors[] = '<span class="error">ERROR: Missing Name </span><br/>';
    }
    if (empty($phone) || empty($email)) {
        $errors[] = '<span class="error">ERROR: You must insert a phone number or email</span><br/>';
    }
    if (!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        $errors[] = '<span class="error">ERROR: Please Insert a valid Email</span><br/>';
    }
    if ($errors) {
        echo 'There were some errors: ';
        echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
    } else {
        mail( "anEmail@hotmail.com", "Monthly Specials Email",
            "Name: $name\n".
            "Email: $email\n".
            "Phone Number: $phone\n".
            "Comment: $comment", "From: $email");
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}

With this you can check all requirements and report all errors and not just the first one.


use:

$error = 0;
if(empty($var1)){ $error = 1; }
if(empty($var2)){ $error = 1; }
if(empty($var3)){ $error = 1; }
if(empty($var4)){ $error = 1; }
if(empty($var5)){ $error = 1; }

if($error > 0)
{
   // Do actions for your errors
}
else
{
   // Send Email
}


you can use try...catch statements for error checking like this. whenever you encounter a condition where an error should be generated, you can use throw new Exception clause.


Use a dirty flag. Check them all and append the message to the dirty variable.


Try this:

if(isset($_POST['submit'])) {
    $errors = array();

    if (empty($name)) {
        $errors[] = 'Missing Name';
    }
    if(empty($phone) || empty($email)) {
        $errors[] = 'You must insert a phone number or email';
    }
    if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        $errors[] = 'Please Insert a valid Email';
    }

    if (!empty($errors)) {
        for ($i = 0; i < count($errors); $i++) {
            echo sprintf('<span class="error">ERROR: %s</span><br/>', $errors[$i]);
        }
    }  else {
        mail( "anEmail@hotmail.com", "Monthly Specials Email",
                                            "Name: $name 
                                             Email: $email 
                                             Phone Number: $phone
                                             Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
     }
}


if(isset($_POST['submit'])) {
    $valid = true;
    if (empty($name)) {
        echo'<span class="error">ERROR: Missing Name </span><br/>';
        $valid = false;
    }  

    if(empty($phone) || empty($email)) {
        echo'<span class="error">ERROR: You must insert a phone number or email</span><br/>';
        $valid=false;
    } 

    if(!preg_match('/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/', $email)) {
        echo'<span class="error">ERROR: Please Insert a valid Email</span><br/>';
        $valid = FALSE;
    }  

    if($valid) {
        mail( "anEmail@hotmail.com", "Monthly Specials Email",
            "Name: $name 
            Email: $email 
            Phone Number: $phone
            Comment: $comment", "From: $email" );
        echo'<span id="valid">Message has been sent</span><br/>';
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号