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

Contact form help...

  • Hello,

    I recently went through Chris' form tutorial. I've got it to validate and I've got the style that I want. However, I can't seem to get the PHP to give the form functionality.

    Here's the PHP:

    <?php

    // CHANGE THE THREE VARIABLES BELOW

    $EmailFrom = \"greyfox135@gmail.com\";
    $EmailTo = \"greyfox135@gmail.com\";
    $Subject = \"Contact Form Submission\";

    $Name = Trim(stripslashes($_POST['Name']));
    $Location = Trim(stripslashes($_POST['Location']));
    $Email = Trim(stripslashes($_POST['Email']));
    $Message = Trim(stripslashes($_POST['Message']));

    // prepare email body text
    $Body = \"\";
    $Body .= \"Name: \";
    $Body .= $Name;
    $Body .= \"\n\";
    $Body .= \"Location: \";
    $Body .= $Location;
    $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 \"<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.html\">\";
    }
    else{
    print \"<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">\";
    }
    ?>


    When I try the form, I get this error: "Parse error: syntax error, unexpected T_STRING in /home/content/f/a/u/faulkventures/html/contactengine.php on line 35"

    Line 35 is this part of the PHP:

    print \"<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.html\">\";


    The file name is correct and is in the correct directory.


    Here is the FORM HTML:

    <form method=\"post\" action=\"contactengine.php\" id=\"commentForm\">
    <fieldset>

    <div id=\"formLeft\">
    <label for=\"Name\">Name:</label>
    <div class=\"input-bg\">
    <input type=\"text\" id=\"Name\" name=\"Name\" class=\"required\" minlength=\"2\" />
    </div>

    <label for=\"Location\">Location:</label>
    <div class=\"input-bg\">
    <input type=\"text\" id=\"Location\" name=\"Location\" class=\"required\" minlength=\"2\" />
    </div>

    <label for=\"Email\">Email:</label>
    <div class=\"input-bg\">
    <input type=\"text\" id=\"Email\" name=\"Email\" class=\"required email\" />
    </div>
    </div>

    <div id=\"formRight\">
    <label for=\"Message\">Message:</label>
    <div class=\"message-bg\">
    <textarea name=\"Message\" id=\"Message\" rows=\"20\" cols=\"20\" class=\"required\"></textarea>
    </div>
    <br />
    <input type=\"image\" src=\"assets/form/sendbtn.jpg\" name=\"submit\" value=\"Submit\" class=\"submit-button\" />
    </div>

    <div class=\"clear\"></div>

    </fieldset>
    </form>


    I am new to web design and this is my first attempt at a contact form. Any help would be greatly appriciated!

    Thanks!

    PS. Here is the site I'm working on: http://faulknerventures.com/contact
  • that form looks like mine......

    anyway, you never \ your "
    this will work, if your file is there and its not in another directory:
    print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.html">";

    in your print double quotes you had other double quotes, so this confused the php, you have to escape some:
    hence the =\"
    You will also have to fix line 38 for it all to work, but you can follow what I changed.

    as an extra why not add another email part thanking people for using your contact form??

    her is my full script including sending robots to google, populating the email, and sending the email to me and a confirmation email to the form filler, with a redirect if successful or not:

    <?php

    ini_set(\"sendmail_from\", \"root@honeycomb-web.co.uk\");

    // get posted data into local variables
    $EmailTo = \"info@honeycomb-web.co.uk\";
    $Subject = \"Website Design\";
    $name = $_REQUEST['name'];
    $tel = $_REQUEST['tel'];
    $email = $_REQUEST['email'];
    $message = $_REQUEST['message'];

    // validation
    $validationOK=true;
    if (!$validationOK) {
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://www.honeycomb-web.co.uk/error.html\\">\";
    exit;
    }

    // prepare email body text
    $Body = \"\";
    $Body .= \"Name: \";
    $Body .= $name;
    $Body .= \"\n\";
    $Body .= \"Tel: \";
    $Body .= $tel;
    $Body .= \"\n\";
    $Body .= \"Details: \";
    $Body .= $message;
    $Body .= \"\n\";

    if($email == \"\") {
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://www.google.com\\">\";
    exit;
    }

    // send email
    $success = mail($EmailTo, $Subject, $Body, \"From: <$email>\", \"-froot@honeycomb-web.co.uk\");

    // redirect to success page
    if ($success){
    //sending mail to the person who filled in the form if they filled in the email
    mail($email, $Subject, \"$name\n\nThank you for contacting honeycomb-web.\nWe will contact you as soon as possible regarding your query.\", \"From: <$EmailTo>\", \"-froot@honeycomb-web.co.uk\");
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://www.honeycomb-web.co.uk/thanks.html\\">\";
    }
    else{
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://www.honeycomb-web.co.uk/error.html\\">\";
    }
    ?>


    EDIT: try sending your contact form with no details, your fields required are not at the correct places, something you might want to look into
  • @ikthius,

    Awesome!! Thank you very much. This has made my day.

    I know about the field errors; thanks for catching that :D

    PHP is quite impressive.
  • On an other note:

    If I wanted to have the form e-mail 2 different address could I just add the second address like this:

    $EmailTo = \"email1@domain.com\"; \"email2@domain.com\";


    ??

    If not, any suggestions?
  • "GreyFox135" said:
    On an other note:

    If I wanted to have the form e-mail 2 different address could I just add the second address like this:

    $EmailTo = \"email1@domain.com\"; \"email2@domain.com\";


    ??

    If not, any suggestions?


    I thinks so, never tried it, but I just add another mail function, with a different address.

    in the if($success)
    I have this on a clients page:

    // send email
    $success = mail($EmailTo, $Subject, $Body, \"From: <$email>\", \"-froot@honeycomb-web.co.uk\");

    // redirect to success page
    if ($success){

    //send copy to me as well, this is the second email, you can add as many as you like here with the mail function
    $success = mail(\"ikthius@gmail.com\", $Subject, $Body, \"From: <$email>\", \"-froot@honeycomb-web.co.uk\");

    //sending mail to the person who filled in the form if they filled in the email
    mail($email, $Subject, \"$name\n\nThank you for contacting honeycomb-web.\nWe will contact you as soon as possible regarding your query.\", \"From: <$EmailTo>\", \"-froot@honeycomb-web.co.uk\");
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=http://www.honeycomb-web.co.uk/thanks.html\\">\";
    }
  • Thank you so much!!
  • "GreyFox135" said:
    Thank you so much!!


    your welcome, hope it works the way your wanting