Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Contact Form: Again…

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #27685
    Travis Love
    Participant

    So a while back I had asked for some help with building a simple contact form in a standard HTML page, and I got some useful help but alas I am back again. So far after a long uphill battle I have gotten jQuery-validate to do on “ok” job validating my fields (except the textarea) and got the form to send to my contactengine.php. The PHP file does its thing and actually sends the email (which oddly takes a long while to receive). The problem now is the email is messing the ’email’ and ‘message’ values, and I also need to implement a PHP validation system.

    Here is my HTML:






































    And here is my extremely butchered PHP:



    function check_input($data)
    {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }

    // CHANGE THE VARIABLES BELOW

    $EmailFrom = "[email protected]";
    $EmailTo = "my_email";
    $Subject = "Love Medical Contact Form Submission";

    $Name = check_input($_POST);
    $Tel = check_input($_POST);
    $Email = check_input($_POST);
    $Message = check_input($_POST);

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "n";
    $Body .= "Tel: ";
    $Body .= $Tel;
    $Body .= "n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "n";
    $Body .= "Message: ";
    $Body .= $Message;
    $Body .= "n";


    // send email
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

    // redirect to success page
    // CHANGE THE URL BELOW TO YOUR "THANK YOU" PAGE
    if ($success){
    print "";
    }
    else{
    print "";
    }
    ?>

    For something people say is so simple, why are there no simple PHP contact templates for people to work from? I am finding it increasingly frustrating that there is no documentation to help newbie designers to learn how to do the “simplest” of things like this. I am feeling like I shouldn’t even design for people if I can’t even offer simple functionality like this.

    So I need help (or direction) in PHP validation, getting my code to send correctly, and I am not to keen on “redirecting” users. I was wondering how to AJAX the error or success messages from the PHP file. I know there is a lot here, but any type of direction would be hugely appreciated.

    Thanks

    #77033
    cybershot
    Participant

    I don’t know enough about php to help you. I know that you can use html 5 for form validation without the need for javascript. I hear there is even a script that you can download to allow non compatible browsers to work.

    check out this page
    html5 form validation

    here is the php script that I use and it seems to work okay. You will need to make an error page and a success page to redirect your users to


    // Website Contact Form Generator
    // http://www.tele-pro.co.uk/scripts/contact_form/
    // This script is free to use as long as you
    // retain the credit link

    // get posted data into local variables
    $EmailFrom = "Pizza and Pasta site";
    $EmailTo = "Your Email Here";
    $Subject = "contact form information";
    $name = Trim(stripslashes($_POST));
    $surname = Trim(stripslashes($_POST));
    $email = Trim(stripslashes($_POST));
    $city = Trim(stripslashes($_POST));
    $comments= Trim(stripslashes($_POST));

    // validation
    $validationOK=true;
    if (Trim($contactName) || Trim($surname) || Trim($contactEmail) || Trim($ContactCity) == " ")
    {
    $validationOK = false;
    }

    if (!$validationOK)
    {
    print "";
    exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $name;
    $Body .= "n";
    $Body .= "Surname:";
    $Body .= $surname;
    $Body .= "n";
    $Body .= "Email: ";
    $Body .= $email;
    $Body .= "n";
    $Body .= "City: ";
    $Body .= $city;
    $Body .= "n";
    $Body .= "Comments: ";
    $Body .= $comments;
    $Body .= "n";

    // send email
    $success = mail($EmailTo, $Subject, $Body . "From: " . $EmailFrom);

    // redirect to success page
    if ($success){
    print "";
    }
    else{
    print "";
    }
    ?>

    #76986
    ralc
    Participant

    for the php validation part you can use php’s filter_var function:



    $ef_name = filter_var($_POST, FILTER_SANITIZE_STRING);
    $ef_email = filter_var($_POST, FILTER_VALIDATE_EMAIL);
    $ef_message = filter_var($_POST, FILTER_SANITIZE_STRING);
    $ef_telephone = filter_var($_POST, FILTER_SANITIZE_STRING);

    You will notice in the code im validating the email using the ‘FILTER_VALIDATE_EMAIL’ filter. And its as simple as that, anything which doenst pass the filter_var will be assigned a value of false.

    #76987
    ralc
    Participant

    also to send the mail i usually use phpmailer rather then the php mail functions. Once you download the files you can send mail by using the following code.



    require 'PHPMailer_v5.1/class.phpmailer.php';
    $mail = new PHPMailer();
    $email_body = "

    Someone has submitted your contact form

    ";
    $email_body .= $cf_message;
    $email_body .= "

    My Email is: ". $cf_email ."

    ";

    $mail->AddReplyTo($cf_email, 'Website Enquirer');
    $mail->AddAddress([email protected]', 'My site admin');
    $mail->SetFrom($cf_email, 'Website enquirer');
    $mail->Subject = 'Contact form submission';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    $mail->MsgHTML($email_body);
    $mail->Send();

    Another thing i like to do is store each contact form submission in a database so every submission is saved just in case something went wrong with the email.

    #76495
    Travis Love
    Participant

    Hey thanks for all the help. I will get all this together and see if it works out. Is there a way to not have to do a page redirect, like send a success or die command through .ajax?

    #76379
    ralc
    Participant

    you can do that in ajax, or you can just post back to the page your on. On contact forms that appear in modal windows (lightboxes) i often use ajax to send the data to avoid the modal window closing. But if the form is just on a page i normally just post back to that page.

    #76300
    Chris Coyier
    Keymaster

    In your HTML, both the email and message INPUTs don’t have “name” attributes, which is required for them to properly pass the data from the form submission. Add those, and make sure they match what you are looking for when you gather the $_POST, and you are set.

    I agree, it’s confusing stuff.

    #76275
    Travis Love
    Participant

    Ahh thanks Chris, that makes sense. I will fix that, and see if that works.

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Back End’ is closed to new topics and replies.