treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Help me write this code please

  • 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 '<div id="sentMessage">There was a problem. Please check your form and try again.</div>';
    return false;
    }

    function startForm(){
    //get the wordpress database information
    global $data;
    //if the submit button is pressed on the form
    if(isset($_POST['sendMessage'])){
    $contactName = strip_tags($_POST['contactName']);
    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['surname']);
    $email = $_POST['contactEmail'];
    $city = strip_tags($_POST['contactCity']);
    $messages = strip_tags($_POST['message']);
    //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['contactsubject'];
    $headers = 'From: ' . $data['contactfrom'] . "\r\n" .
    'Reply-To: ' . get_bloginfo('admin_email') . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


    $recipient = $data['reservationemail'];

    if(mail($recipient, $subject, $contact, $headers)){
    echo '<div id="sentMessage">Your message was sent</div>';
    }
    }
    }
    }

    $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
  • What exactly is the problem if it is running? I am not sure i am following you here...
  • 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.
  • 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.
  • that did not help. It does the same thing.
  • Calling "return false" in your validationFalse() method won't stop your startForm() method. You have to put in the startForm() method.