Question

dunno can I write one more question about contact form but i will try :) Maybe u will help faster ;p

Code:

<?php
 $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
 $from = 'From: domain.com'; 
 $to = 'dawid@domain.com'; 
 $subject = 'Message from domain.com';

 $body = "From: $name\n E-Mail: $email\n Message:\n $message";

   if ($_POST['submit']) {
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
        {
               echo '<p class="msg">Wrong email adress!!!</p>';
        }            

        if ($name != '' && $email != '') {               
                if (mail ($to, $subject, $body, $from)) { 
                        echo '<p class="msg">Your message has been sent!</p>';
                } else { 
                        echo '<p class="msg">Something went wrong, go back and try again!</p>'; 
                       } 
                } else {
                        echo '<p class="msg">You need to fill in all required fields!!</p>';
                       }
    }
?>  

And as u can see i have here few messages (echos) in if statements. But I wanna make this to display just one echo even if there would be more errors in contact form... Is that possible ?

example: Somebody leave all inputs empty and click "send" - atm he would get msg about wrong email and "u need fill all required fields". But in this kind os situation firstly i would like to display just msg about empty fields and after that if for example somebody would write wrong email another msg about wrong email :D Hope u understand what I mean. Thanks

Was it helpful?

Solution 3

if ($_POST['submit']) {

    if (!$email) {
        echo '<p class="msg">Email is required.</p>';
    }
    elseif (!$name) {
        echo '<p class="msg">Name is required.</p>';
    }
    elseif (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
        echo '<p class="msg">Wrong email adress!!!</p>';
    }
    elseif ($name != '' && $email != '') {

        if (mail ($to, $subject, $body, $from)) { 
            echo '<p class="msg">Your message has been sent!</p>';
        }
        else { 
            echo '<p class="msg">Something went wrong, go back and try again!</p>'; 
        } 
    }
    else {
        echo '<p class="msg">You need to fill in all required fields!!</p>';
    }

}

OTHER TIPS

if ($name == '' || $email == '' || $message == '') {               
 echo '<p class="msg">You need to fill in all required fields!!</p>';
}else if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
 echo '<p class="msg">Wrong email adress!!!</p>';
}else if (!mail ($to, $subject, $body, $from)){
 echo '<p class="msg">Your message has not been sent!</p>';
}else{
 echo '<p class="msg">Your message has been sent!</p>';
}

Make an array of errors and then implode that array to display the complete error message.

// DEFAULT
$errors = array();

// CHECK THINGS FOR ERRORS AND ADD THEM TO THE ERROR ARRAY
if ((!$name) || ($name == '')) {
    $errors[] = '* Name is required';
}

if ((!$email) || ($email == '')) {
    $errors[] = '* Email is required';
}

// ... etc. - Anything else you want caught in an error

// IF WE HAVE ERRORS, TURN THEM INTO A STRING AND PRINT THEM
if ($errors) {
    $error_message = implode('<br>', $errors);
    echo $error_message;
}

EDIT: Per request of OP, here is the example that was asked for.

// CHECK THINGS FOR ERRORS AND ADD THEM TO THE ERROR ARRAY
if ((!$name) || ($name == '')) {
    $errors = '* Name is required';
}

if ((!$email) || ($email == '')) {
    $errors = '* Email is required';
}

// ... etc. - Anything else you want caught in an error

// IF WE HAVE ERRORS, PRINT THEM
if ($errors) {
    echo $error_message;
}

** Note: You can achieve the same thing (and it would make more sense to do so) by using conditional statements. Only one condition can be evaluated as true, so once it finds a match, it will not go into any other 'elseif' statements.

I still think that it's possible to edit this code as it should work as i want... but dont know how ;D

<?php
 $name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
 $from = 'From: domain.com'; 
 $to = 'dawid@domain.com'; 
 $subject = 'Message from domain.com';

 $body = "From: $name\n E-Mail: $email\n Message:\n $message";

   if ($_POST['submit']) {
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
        {
               echo '<p class="msg">Wrong email adress!!!</p>';
        }            

        if ($name != '' && $email != '') {               
                if (mail ($to, $subject, $body, $from)) { 
                        echo '<p class="msg">Your message has been sent!</p>';
                } else { 
                        echo '<p class="msg">Something went wrong, go back and try again!</p>'; 
                       } 
                } else {
                        echo '<p class="msg">You need to fill in all required fields!!</p>';
                       }
    }
?>                                 

maybe put this error about mail in other place or something ? or any else if ? help <3

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top