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

Contact Form

  • hey there peeps.

    Im trying to implement a contact form that uses php and then redirects to an error or success page.

    What im getting stuck with is getting it to work inside wordpress.

    Im trying to use the contact form chris setup a while back, well using the php code for it anyways.

    Can anyone shed some light for me please.

    Thanks

    Here is the code im using:

    The page template:

    <?php
    /*
    Template Name: Contact Form Page
    */
    ?>

    <?php get_header(); ?>

    <div id=\"main-content\">

    <div id=\"contact-area\">

    <h2>Contact Blindeye Films:</h2>

    <br>

    <form method=\"post\" action=\"contactengine.php\">
    <label for=\"Name\">Name:</label>
    <input type=\"text\" name=\"Name\" id=\"Name\" />

    <label for=\"Tel\">Tel:</label>
    <input type=\"text\" name=\"Tel\" id=\"Tel\" />

    <label for=\"Email\">Email:</label>
    <input type=\"text\" name=\"Email\" id=\"Email\" />

    <label for=\"Message\">Message:</label><br />
    <textarea name=\"Message\" rows=\"20\" cols=\"20\" id=\"Message\"></textarea>

    <input type=\"submit\" name=\"submit\" value=\"Submit to Blindeye Films\" class=\"submit-button\" />
    </form>

    <div style=\"clear: both;\"></div>


    </div><!-- end contact-area -->

    <?php get_sidebar(); ?>

    </div><!-- end main-content -->

    <?php get_footer(); ?>


    The contact form php code:
    <?php

    $EmailFrom = \"enter email here\";
    $EmailTo = \"\";
    $Subject = \"Enquiry reply from Blindeye Films\";
    $Name = Trim(stripslashes($_POST['Name']));
    $Tel = Trim(stripslashes($_POST['Tel']));
    $Email = Trim(stripslashes($_POST['Email']));
    $Message = Trim(stripslashes($_POST['Message']));

    // 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 .= \"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
    if ($success){
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=contactthanks.php\\">\";
    }
    else{
    print \"<meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">\";
    }
    ?>


    And the redirect page:
    	
    <div id=\"main-content\">


    <h1>Your message has been sent!</h1><br />

    <p><a href=\"/\">Go Home</a></p>

    </div><!-- end main-content -->

  • I don't see anything wrong with a quick overview, althouguh using a meta redirect sounds weird....

    Looking at what your goal is, you may wanna give "WP Contact Form" a shot. It certainly did the job for me.
    In the adminpanel you can set up a thank you, and fail message.

    Also, its' very secure (does all kinds of trimms and stript for possible injections). And it comes with a language file that one can use. Light weight, secure and customizable.

    I say try it, it's free
    Check here
  • the problem may be the location of the action file contactengine.php

    Try using a full URL to define its location
  • make sure contactengine.php is in a directory you know of e.g. yoursite.com/scripts
    then have your form sees where it really is yoursite.com/scripts/contactengine.php

    then make sure your success & error pages are shown exactly, then you wont have to many problems...

    if your on a windows server you may need to set your ini in the contactengine.php (this is needed by my server on one of my sites)