这个if或者如果是联系方式

时间:2014-02-12 17:32:32

标签: php html forms email contact

dunno我可以再写一个关于联系表格的问题,但我会尝试:)也许你会帮助更快; p

代码:

<?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>';
                       }
    }
?>  

正如你可以看到我在if语句中有几条消息(回声)。但我想让它显示一个回声,即使联系表格中会有更多错误......这可能吗?

例如: 有人将所有输入都留空并点击“发送” - 他会收到有关错误电子邮件的消息和“你需要填写所有必填字段”。 但在这种情况下,首先我想显示关于空字段的消息,之后如果有人会写错了电子邮件另一个关于错误电子邮件的消息:D希望你理解我的意思。感谢

4 个答案:

答案 0 :(得分:1)

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>';
}

答案 1 :(得分:1)

创建一个错误数组,然后内爆该数组以显示完整的错误消息。

// 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;
}

编辑: 根据OP的请求,以下是要求的示例。

// 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;
}

**注意:通过使用条件语句,您可以实现相同的功能(并且这样做会更有意义)。只有一个条件可以被评估为真,所以一旦找到匹配,它就不会进入任何其他'elseif'语句。

答案 2 :(得分:0)

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>';
    }

}

答案 3 :(得分:-1)

我仍然认为可以编辑这段代码,因为它应该按我的意愿工作......但不知道如何; 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>';
                       }
    }
?>                                 

也许把这个错误关于邮件放在其他地方或什么的?或者其他什么?帮助&lt; 3