Skip navigation
sam2322
Currently Being Moderated

PHP Contact Form Help

Jun 28, 2013 9:04 AM

Tags: #php #dreamweaver #form #cs6 #contact

I am trying to link a contact form on my website with my email address, as well as have it direct me to an already created thank you page upon submission. I have followed numerous PHP coding tutorials, but no matter what I try when I click the "submit" button to test it out the page just redirects to the text of my .php file and doesn't email the form to me. I'm not sure if it's a problem with the way the php is coded or with the action on the form itself. Please help! This is what shows up when I click submit (for the code I've most recently tried, without trying to connect to the thank you page):

 

<?php

 

/* Subject and Email Variables */

 

$emailSubject = 'Participant Contact Form';

$webMaster = 'xxx@xxx.com';

 

/* Gathering Data Variables */

 

$nameField = $_POST('name');

$ageField = $_POST('age');

$phoneField = $_POST('phone');

$emailField = $_POST('email');

 

$body = <<<EOD

<br><hr><br>

Name: $name <br>

Child Date of Birth: $age <br>

Phone Number: $phone <br>

Email: $email <br>

EOD;

 

$headers = "From: $email\r\n";

$headers .= "Content-type: text/html\r\n";

$success = mail($webMaster, $emailSubject, $body, $headers);

 

?>

 
Replies
  • Currently Being Moderated
    Jun 28, 2013 1:57 PM   in reply to sam2322

    >the page just redirects to the text of my .php

     

    Not sure what you mean, but if it's that you are seeing php code on your web page, then one possibility is that php is not enabled on your server. Are you sure it is supported with your hosting plan?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 28, 2013 8:56 PM   in reply to bregent
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 17, 2013 10:46 PM   in reply to sam2322

    Hi Sam2322, I feel your pain. I had exactly the same problem with several forms and their accompanying PHP scripts. It's not a server problem. It's purely to do with the interaction between your form and the PHP script.

     

    This is the PHP script I ended up using. It works for me AND I HOPE IT WORKS FOR YOU TOO.  It has a honey trap included to reduce the occurrence of SPAM.

    <?php

    $name = '';    

    $subject = '';   

    $email = '';

    $message = '';

       

    function getIp()

    {if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){

        $ip_address=$_SERVER['HTTP_X_FORWARDED_FOR'];

    }

     

    if (!isset($ip_address)){

            if (isset($_SERVER['REMOTE_ADDR']))   

            $ip_address=$_SERVER['REMOTE_ADDR'];

    }

    return $ip_address;

    }

     

     

    //taking the data from form   

     

    $name = addslashes(trim($_POST['name']));   

    $subject = addslashes(trim($_POST['subject']));   

    $email = addslashes(trim($_POST['email']));

    $message = addslashes(trim($_POST['message']));

     

    //form validation

    $errors = array();

    $fields = array();

    if(!$name) {

        $errors[] = "Please enter your name.";

        $fields[] = "name";

    }

    $email_pattern = "/^[a-zA-Z0-9][a-zA-Z0-9\.-_]+\@([a-zA-Z0-9_-]+\.)+[a-zA-Z]+$/";

    if(!$email) {

        $errors[] = "Please enter your e-mail address.";

        $fields[] = "email";

    } else if(!preg_match($email_pattern, $email)) {

        $errors[] = "The e-mail address you provided is invalid.";

        $fields[] = "email";   

    }

    if(!$subject) {

        $errors[] = "Please choose the subject of your message.";

        $fields[] = "subject";

    }

    if(!$message) {

        $errors[] = "Please enter your message.";

        $fields[] = "message";

    }

     

    //preparing mail

    if(!$errors) {

        //taking info about date, IP and user agent

        $timestamp = date("Y-m-d H:i:s");

        $ip   = getIp();

        $host = gethostbyaddr($ip);

        $user_agent = $_SERVER["HTTP_USER_AGENT"];

     

        $headers = "MIME-Version: 1.0\n";

        $headers .= "Content-type: text/html; charset=utf-8\n";

        $headers .= "Content-Transfer-Encoding: quoted-printable\n";

        $headers .= "From: $email\n";

     

        $content = 'Subject: '.$subject.'<br>'.

        'Name: '.$name.'<br>'.

        'E-mail: '.$email.'<br>'.

        'Message: '.$message.'<br>'.

        'Time: '.$timestamp.'<br>'.

        'IP: '.$host.'<br>'.

        'User agent: '.$user_agent;   

     

        //sending mail

        $ok = mail("PUT THE REQUIRED EMAIL ADDRESS HERE","Message from ADD YOUR DOMAIN NAME HERE", $content, $headers);

        if($ok) {

            $response['msgStatus'] = "ok";

            $response['message'] = "Thank you for your enquiry. A member of our team will be in touch within 24 business hours.";

        } else {

            $response['msgStatus'] = "error";

            $response['message'] = "An error occured while trying to send your message. Please try again later.";

        }

    } else {

        $response['msgStatus'] = "error";

        $response['errors'] = $errors;

        $response['errorFields'] = $fields;

    }

     

    header('Content-type: application/json');

    echo json_encode($response);

    $robotest = $_POST['robotest']; 

    if($robotest){

      we are dealing with a bot

    else{

      we are dealing with a human

    }

    ?>

     

    and here is the form that goes with it

     

     

    <form action="send-contact.php" method="post" class="contact-form">

     

                    <p class="half">

                        <label for="name">Name</label>

                      <input name="name" class="half" id="name">

                    </p>

                    <!-- The following field is for robots only, invisible to humans: -->

    <li class="robotic" id="pot">

      <label for="robotest">Middle Name</label>

      <input name="robotest" id="robotest" class="robotest" type="text" />

    </li>

                    <p class="half">

                      <label for="email">E-mail</label><input name="email" id="email">

                    </p>

                    <p>

                        <label for="subject">Topic</label><select name="subject" id="subject">

                            <option value="0">Choose</option>

                            <option value="1">YOU CUSTOMISE THESE</option>

                            <option value="2">YOU CUSTOMISE THESE</option>

                            <option value="3">YOU CUSTOMISE THESE</option>

                            <option value="4">YOU CUSTOMISE THESE</option>

                            <option value="4">YOU CUSTOMISE THESE</option>

                        </select>

                    </p>

                    <p>

                        <label for="message">Message</label><textarea name="message" id="message" rows="5" cols="20"></textarea>

                    </p>

                    <p><button name="send" type="submit" value="1">Send message</button></p>

                </form>

     

    YOU'LL HAVE TO USE YOUR OWN CSS TO MAKE THE FORM LOOK THE WAY YOU WANT AND INCLUDE THIS BIT OF CODE IN THE CSS (THIS HIDES THE FORM FIELD FROM HUMANS TO FOIL SPAMBOTS)

     

    }

    .robotic { display:none; }

     

    GOOD LUCK!

     


     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points