如果不满足条件,则中断if / else语句

时间:2016-05-17 18:31:08

标签: php if-statement

我有一个表单,我使用php来处理,除非条件不满足时才有效。我没有使用captia而是隐藏了一个普通的字段。只有在隐藏字段为空时才会运行该过程。否则它会被打破。如果$ hide(隐藏字段)=什么都可以,但是当填写字段时它会抛出错误。

<?php
$to      = 'test@test.com';
$email   = $_POST['email'];

$name  = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['comments'];
$hide = $_POST['message'];
$error=array();
if ($hide!=''){
    break;
}else{
    if($_POST['name']==''){
        $error[]="Your name is required";
    }
    if($_POST['email']==''){
        $error[]="Your email is required";
    }
    if($_POST['subject']==''){
        $error[]="Subject is required";
    }
    if($_POST['comments']==''){
        $error[]="Comments is required";
    }

    if(count($error)>0){
        echo "<div class='alert alert-danger'>";
        foreach($error as $data)
        {
            echo "<p>".$data."</p>";
        }
        echo "</div>";
        die();
    }

    $headers = 'From: Attorney Website'. '<'.$email.'>' . "\r\n" .
        'Reply-To: '. $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    if(mail($to, $subject, $message, $headers))
    {
        echo "<div class='alert alert-success'>You message has been succesfully received. We will reply you soon.</div>";
        die();
    } else {
        echo "<div class='alert alert-danger'>Opps! Something went wrong. Please try again.</div>";
        die();
    }
}
?>

1 个答案:

答案 0 :(得分:2)

您不需要使用break

替换:

if ($hide!=''){
    break;
}
else {

只是

if ($hide) {
   // run email code

这将确保$ hide在继续之前包含一些内容