嵌套if语句不起作用

时间:2015-12-09 17:08:08

标签: php forms

我无法弄清楚出了什么问题。我的表格有两个目的。通过电子邮件发送基本数据,或者如果访问者想要目录,则显示其他字段以获取更多数据。

其他字段的验证代码 - 地址,城市,州,邮政编码 - 阻止代码完成并发送电子邮件。每个字段的验证工作正常,但选中复选框后,不会发送任何邮件。没有它检查,它的工作原理,如果我删除它的部分,它的工作原理。

我把它缩小了,但无法理解。任何帮助表示赞赏。

sanitize()函数位于包含的helper.php文件中。

if (isset($_POST['action']) && $_POST['action'] === 'submit_contact_info') {

    $name = sanitize($_POST['name']);
    $email = sanitize($_POST['email']);
    $telephone = sanitize($_POST['telephone']);
    $form_message = sanitize($_POST['message']);
    $address = sanitize($_POST['address']);
    $city = sanitize($_POST['city']);
    $state = sanitize($_POST['state']);
    $zip = sanitize($_POST['zip']);

    // Store visitor name in SESSION variable for use @thankyou
        $_SESSION['name'] = $name;  

    // Check if fields have input
    if(empty($name))
    {
        $errMsg['name'] = '*Please enter your name.';
    }
    elseif(empty($email))
    {
        $errMsg['email'] = '*Please enter your email address.';
    }
    elseif(empty($telephone)) 
    {
        $errMsg['telephone'] = '*Please enter your telephone number.';
    }   
        elseif (isset($_POST['catalog_yes'])){
            // Check if mailing  fields have input
            if(empty($address))
            {
                $errMsg['address'] = 'Please enter your address.';
            }
            elseif(empty($city))
            {
                $errMsg['city'] = 'Please enter your city.';
            }
            elseif(empty($state))
            {
                $errMsg['state'] = 'Please enter your state.';
            }
            elseif(empty($zip))
            {
                $errMsg['zip'] = 'Please enter your zip code.';
            }
        } 
        else {        
        // Prepare message for e-mail; set e-mail recipient  
        $jim_gmail = 'jim@gmail.com';

        $to = $jim_gmail;
        $subject = 'Web site inquiry';
        $from = $email;
        $message = '
        <html>
        <head></head>
        <body>
        <h2>Web site message via Contact Form</h2>
        <p>Name: ' . $name . '<br>
           Email: ' . $email . '<br>
           Telephone: ' . $telephone . '<br>
           Message: ' . $form_message . '<br>
           Adress: ' . $address . '<br>
           City: ' . $city . '<br>
           State: ' . $state . '<br>    
           Zip Code: ' . $zip . '<br>    
        </body>
        </html>
        '; // end of message
        // To send HTML mail, the Content-type header must be set
        $headers = 'MIME-Version: 1.0' . "\r\n";      // code to send HTML on UNIX
        $headers .= 'Content-type:text/html; charset=iso-8859-1' . "\r\n";

        // Additional headers
        $headers .= 'From: ' . $from . "\r\n";

        // Send message using mail() function 
        mail($to, $subject, $message, $headers);

        header('Location: thankyou.php');
        exit();
    }
}

1 个答案:

答案 0 :(得分:1)

你已经把条件

elseif (isset($_POST['catalog_yes'])){

} else {
  //The final code, which will never run if $_POST['catalog_yes'] is set
}

因此,如果条件允许,您需要移动您在最终ELSE之外的代码。只有一个这样的条件:

if(!count($errMsg)) {
   // Prepare message for e-mail; set e-mail recipient  
    $jim_gmail = 'jim@gmail.com';

    $to = $jim_gmail;
    $subject = 'Web site inquiry';
    $from = $email;
    $message = '
    <html>
    <head></head>
    <body>
    <h2>Web site message via Contact Form</h2>
    <p>Name: ' . $name . '<br>
       Email: ' . $email . '<br>
       Telephone: ' . $telephone . '<br>
       Message: ' . $form_message . '<br>
       Adress: ' . $address . '<br>
       City: ' . $city . '<br>
       State: ' . $state . '<br>    
       Zip Code: ' . $zip . '<br>    
    </body>
    </html>
    '; // end of message
    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";      // code to send HTML on UNIX
    $headers .= 'Content-type:text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'From: ' . $from . "\r\n";

    // Send message using mail() function 
    mail($to, $subject, $message, $headers);

    header('Location: thankyou.php');
    exit();
}