Grow your CSS skills. Land your dream job.

Help me write this code please

  • # April 10, 2012 at 9:40 pm

    I have been trying for days to get this contact form working on my site. I have rewritten it several times and tried many different things over the days. I just now tried putting it into a class structure. I am a beginner at best with php


    class form
    {
    function validationFalse(){
    echo '
    There was a problem. Please check your form and try again.
    ';
    return false;
    }

    function startForm(){
    //get the wordpress database information
    global $data;
    //if the submit button is pressed on the form
    if(isset($_POST)){
    $contactName = strip_tags($_POST);
    if( preg_match('/^[a-zA-Z ]+$/', $contactName) == 'false') {
    //if the contact name doesn't validate, call the error function
    $this->validationFalse();
    }
    $surname = strip_tags($_POST);
    $email = $_POST;
    $city = strip_tags($_POST);
    $messages = strip_tags($_POST);
    //check to see if the variables are empty, if they are, just exit

    $contact = "Contact Name: $contactName n";
    $contact .= "Surname: $surname n";
    $contact .= "Email: $email n";
    $contact .= "City: $city n";
    $contact .= "Message: $messages n";

    if(empty($contact)){
    $validation = false;
    echo "You did not fill out the form";
    echo $contactName . $email . $city . $messages;
    }

    $subject = $data;
    $headers = 'From: ' . $data . "rn" .
    'Reply-To: ' . get_bloginfo('admin_email') . "rn" .
    'X-Mailer: PHP/' . phpversion();


    $recipient = $data;

    if(mail($recipient, $subject, $contact, $headers)){
    echo '
    Your message was sent
    ';
    }
    }
    }
    }

    $form = new form;
    $form->startForm();

    It might look a little chopped up. Can you help me clean it up and get it working?

    right now, it runs all functions when I press the submit button. Still calls the error message, and still sends the form and gives the success message

    # April 10, 2012 at 10:00 pm

    What exactly is the problem if it is running? I am not sure i am following you here…

    # April 10, 2012 at 10:33 pm

    the problem is that the validationFalse function is suppose to be called if the contactName does not match the preg_match statement. The function does get called but the rest of the code runs as well and it is not suppose to. So when you pres submit, the form will send, you will see the success message and also the error message. It should not do that.

    # April 10, 2012 at 11:34 pm

    Try changing this line:

    if( preg_match('/^[a-zA-Z ]+$/', $contactName) == 'false') 

    To this line instead:

    if( !preg_match('/^[a-zA-Z ]+$/', $contactName) ) 

    Using the negation operator (!) in front of the preg_match() function will effectively accomplish what you are trying right now…the issue is preg_match() wont return a true or false STRING…which it seems you are checking for, hence, it is returning true and completing the rest of the class functions.

    # April 10, 2012 at 11:47 pm

    that did not help. It does the same thing.

    # April 11, 2012 at 1:02 am

    Calling “return false” in your validationFalse() method won’t stop your startForm() method. You have to put in the startForm() method.

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

You must be logged in to reply to this topic.

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