简体   繁体   中英

Nested if statement that won't work

I cannot figure out what's wrong. My form serves two purposes. Email basic data, or if the visitor wants a catalog, to display additional fields for more data.

Somehow the validation code for the additional fields -- address, city, state, zip -- is preventing the code to finish and send an email. The validation for each field works fine, but when the checkbox is checked, no mail is sent. Without it checked, it works, and if I remove that part it works.

I have narrowed it down, but cannot figure it out. Any assistance is appreciated.

The sanitize() function is in an included helper.php file.

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();
    }
}

You have put the condition

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

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

So you need to move the codes you are putting under final ELSE outside of the else if conditions. There can be only one condition like this :

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM