Grow your CSS skills. Land your dream job.

help with contact code

  • # February 12, 2012 at 2:24 am

    I am having trouble with a bit of code. I can’t explain what is going on here. I am trying to modify some code to work on my site. I have the site looking correct but in order to do that, I had to remove a bit of code that prevented the site from sending an email when the forms fields are empty. Right now, it will send the form when they are empty. I have been trying to add some conditionals into the code to prevent it from sending but for some reason, it isn’t working and I can’t figure out why. Here are a few things I have tried.

    this is my current attempt at setting an boolean variable. I have never really done this so it may be the wrong aproach.



    $set = '0';
    if(!isset($_POST) ||
    !isset($_POST) ||
    !isset($_POST)) {
    $set = 0;
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    } else {
    $set = 1;
    }

    I have also tried



    if(!isset($_POST) ||
    !isset($_POST) ||
    !isset($_POST)) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    die();
    }

    and this


    if( $set == 1 ){
    $headers = 'From: '.$email_from."rn".
    'Reply-To: '.$email_from."rn" .
    'X-Mailer: PHP/' . phpversion();
    mail($email_to, $email_subject, $email_message, $headers);
    }

    no matter what I do, $set is always equal to 1. I can’t figure out why it won’t equal 0. The first conditional should make it zero but it doesn’t.

    Here is the unedited code


    if(isset($_POST)) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "you@yourdomain.com";
    $email_subject = "Your email subject line";


    function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.

    ";
    echo $error."

    ";
    echo "Please go back and fix these errors.

    ";
    die();
    }

    // validation expected data exists
    if(!isset($_POST) ||
    !isset($_POST) ||
    !isset($_POST) ||
    !isset($_POST) ||
    !isset($_POST)) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $first_name = $_POST; // required
    $last_name = $_POST; // required
    $email_from = $_POST; // required
    $telephone = $_POST; // not required
    $comments = $_POST; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.
    ';
    }
    $string_exp = "/^[A-Za-z .'-]+$/";
    if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid
    ';
    }
    if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid
    ';
    }
    if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid
    ';
    }
    if(strlen($error_message) > 0) {
    died($error_message);
    }
    $email_message = "Form details below.nn";

    function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."n";
    $email_message .= "Last Name: ".clean_string($last_name)."n";
    $email_message .= "Email: ".clean_string($email_from)."n";
    $email_message .= "Telephone: ".clean_string($telephone)."n";
    $email_message .= "Comments: ".clean_string($comments)."n";


    // create email headers
    $headers = 'From: '.$email_from."rn".
    'Reply-To: '.$email_from."rn" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
    ?>

    <!-- include your own success html here -->

    Thank you for contacting us. We will be in touch with you very soon.

    < ?php
    }
    ?>

    # February 12, 2012 at 10:50 am

    A post variable will be set even if it is empty, you need to test if they are empty instead. The trim() makes sure they haven’t just put blank space in there.

    // validation expected data exists
    if(trim($_POST)=="" ||
    trim($_POST)=="" ||
    trim($_POST)=="" ||
    trim($_POST)=="" ||
    trim($_POST)=="") {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    }
    # February 12, 2012 at 9:25 pm

    I had the same thought but when I echoed out the $_POST variable, there was nothing there. So I figured it was empty

    # February 12, 2012 at 10:02 pm

    it doesn’t work. No matter what I do. My bool var always returns 1 as well when it should be returning a zero

    # February 12, 2012 at 11:03 pm

    I would alway try to eliminate the largest error first with a condition then narrow down and then do an else for the final action. That way the mail can only proceed if no errors have been flagged

    Try this



    < ?php

    if (isset($_POST) && trim($_POST)!=="") {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "you@yourdomain.com";
    $email_subject = "Your email subject line";

    function died($error) {
    $diemessage="We are very sorry, but there were error(s) found with the form you submitted.
    These errors appear below.

    ".$error."

    Please go back and fix these errors.

    ";
    die($diemessage);
    }

    $first_name = trim($_POST); // required
    $last_name = trim($_POST); // required
    $email_from = trim($_POST); // required
    $telephone = trim($_POST); // not required
    $comments = trim($_POST); // required


    if($first_name=="" || $last_name=="" || $email_from=="" || $telephone=="" || $comments=="") {
    died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.
    ';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";
    if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid
    ';
    }
    if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid
    ';
    }
    if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid
    ';
    }

    if($error_message!=="") {
    died($error_message);
    }
    else { //we didn't have an error to report so go on

    $email_message = "Form details below.nn";

    function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."n";
    $email_message .= "Last Name: ".clean_string($last_name)."n";
    $email_message .= "Email: ".clean_string($email_from)."n";
    $email_message .= "Telephone: ".clean_string($telephone)."n";
    $email_message .= "Comments: ".clean_string($comments)."n";


    // create email headers
    $headers = 'From: '.$email_from."rn".
    'Reply-To: '.$email_from."rn" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);

    echo ('Thank you for contacting us. We will be in touch with you very soon.');

    }

    }
    ?>

    # February 12, 2012 at 11:10 pm

    Else if you want to go the boolean route, encapsulate all the tests in a function and return true or false then you can call it easily to perform a total verification something structurally like



    $error="";

    function all_tests(){
    if (insert test here for not valid email) {
    $error="Email address incorrect.";
    return false;
    }
    if (insert test here for not valid comment) {
    $error="Invalid comment.";
    return false;
    }
    // etc. etc. keep going with conditions.
    return true;
    }


    if (all_tests()) {
    send mail code;
    }
    else {
    die('Sorry something went wrong as follows.

    '.$error);
    }


    # February 12, 2012 at 11:19 pm

    I went the boolean route to test and see if I could figure out why the code wasn’t working the way I thought it should work. I am trying the code you posted in your last message right now. It does appear to be working so far. I ran into trouble when I tried to edit the code to get the sidebar to show. The problem is that I have this code mixed in with the contact page. I was going to set the form to submit to SELF and get the error messages and success message to show on the contact page without having to use an ajax jquery solution. Right now, things look good but the sidebar doesn’t show because of the died() function. I will have to keep messing with it. I guess the error page doesn’t need to have the sidebar. Thanks for the help.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".