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

Simple Contact form

  • Hi All,

    I downloaded Chris's simple contact form, and, since I know pretty much nothing about PHP, I was hoping to get some help..

    Below is my markup:


    <form id="contact-form" action = "contactengine.php" method = "post">

    <fieldset id="texfields" class="grid_4 alpha">
    <ul>
    <li>
    <label for="name">Name</label><br>
    <input id="name" name="name" type="text" placeholder="First and last name" required>

    </li>

    <li>
    <label for="email">Email</label><br>
    <input id="email" name="email" type="email" placeholder="example@domain.com" required>


    </li>

    <li>
    <label for="phone">Phone</label><br>
    <input id="phone" name="phone" type="tel" placeholder="Eg.000.000.0000" required>
    </li>
    </ul>

    </fieldset>

    <fieldset id="comments" class="grid_4 omega">

    <label for="comments">Comments</label><br>
    <textarea id="comments" name="comments" placeholder="Type your message here"required></textarea><br>

    <button type="submit" class="grid_1">Send</button>

    </fieldset>



    </form>



    And this is the PHP that I modified (I was hoping that's what I had to do) to match my markup:

    <?php

    $EmailFrom = "chriscoyier@gmail.com";
    $EmailTo = "info@dreamgem.net";
    $Subject = "Nice & Simple Contact Form by CSS-Tricks";
    $Name = Trim(stripslashes($_POST['name']));
    $Email = Trim(stripslashes($_POST['email']));
    $Phone = Trim(stripslashes($_POST['phone']));
    $Comments = Trim(stripslashes($_POST['comments']));

    // validation
    $validationOK=true;
    if (!$validationOK) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "name: ";
    $Body .= $name;
    $Body .= "\n";
    $Body .= "email: ";
    $Body .= $email;
    $Body .= "\n";
    $Body .= "phone: ";
    $Body .= $phone;
    $Body .= "\n";
    $Body .= "comments: ";
    $Body .= $comments;
    $Body .= "\n";

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

    // redirect to success page
    if ($success){
    print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
    }
    else{
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }
    ?>


    My issues are:

    1. I receive the email, but the content is empty
    2. How do I modify the From field so that it would show actual email it came from instead of static email I set..
    3. I would like to get rid of the re-direct page as I'm using this form on the footer of a website..

    Thanks a bunch in advance and sorry for very basic question!

  • I'm having the same issue with an empty email.. I get the template printed, but the user input is not printed in the email.
  • 1)
    Think about what you're doing.

    $Email and $email are not the same. PHP variables are case sensitive.

    3) Remove the print which redirects to contactthanks.php
  • Hi, I'm having the same problem with an empty email. I removed 'print' which just made the whole thing hang.

    Any advice to get this working?

  • $variable and $Variable ARE NOT the same.

    tip: if you're not getting a bunch of errors like Notice: Undefined variable: name... when you run your original code, then you need to turn on error reporting. It will be a great help with troubleshooting. Turn on error reporting in your php.ini file, or you can do it at the top of your script by using

    error_reporting( -1 );

  • This is a really good contact form if you don't know what you're doing. http://www.fastsecurecontactform.com

  • I'm thinking I'll do a quick upgrade for Chris' form...