PhpTips.im - Everything about php - Download CMS, find tips and tricks, code snippetz, classes and more and more...
Bookmark this page !
   

Articles

    Home » Articles » PHP Classes » A Simple PHP Contact Form Script
Published on : 05.05.2010 Category : PHP Classes Viewed : 63 times.

It’s also a free one and a rather easy one too. You should be able to do it yourself. To create a working contact form, you need the front end and the back end. The front end presents the form to the website user and the back end accepts the data sent from the form by the user and take any necessary actions such as relaying the message to your email.
Front end (HTML)

Simply copy this HTML form and paste it into your web pages where you want it to be, such as on the sidebar so a visitor can send you a contact message on any page.

Code:

<form action="/cf.php" method="post">
    <p><label for="input-email">Your Email: </label><br/><input type="text" name="email" id="input-email" size="40"/></p>
    <p><label for="input-message">Your Message: </label><br/><textarea type="text" name="message" id="input-message" rows="10" cols="40"></textarea></p>
    <p><button type="submit" name="submit">Send</button></p>
</form>

You may also want to style this form a little bit to make it look fancier. But make sure you don’t change any of the properties inside the tags such as action=”…” and name=”…”.
Back end (PHP)

Copy and paste these PHP code into a file named cf.php, change youremail@yoursite.com to your own email address that you wish to receive the messages. Put the file at the root directory of your domain so that everyone can access it at http://www.yoursite.com/cf.php.
Code:

<?php

if (isset($_GET['success'])) {

    ?><p><span style="color:green;">Message successfully sent.</span> Thank you!</p><?php

} else {

    if (!empty($_POST['email']) && !empty($_POST['message'])) {

        $to = 'youremail@yoursite.com'; // your email address, can be @gmail.com, etc.
        $subject = 'Contact from yoursite.com'; // change yoursite.com to your own domain name
        $message = $_POST['message']."rnrnSender IP: ".$_SERVER["REMOTE_ADDR"];
        $headers = 'From: '.$_POST['email']."rn".
                    'Reply-To: '.$_POST['email']."rn";
        mail($to, $subject, $message, $headers);

        header("Location: /cf.php?success"); // redirects the sender to success page so he or she doesn't accidentally send multiple identical messages

    } else {

        ?><p><span style="color:red;">Error detected.</span> Please make sure you have filled all fields. Press back button to go back.</p><?php

    }

}

?>

Should be functioning properly. Thus far this contact form script has been successfully tested on Rackspace Cloud, DreamHost and Linode.

There are a lot more that can be done to create a sophisticated contact form. For starters, you can implement a spam catcher and a file upload control that sends the user uploaded file as attachment to your email. You could also have on-page error detection but that’d need a lot more coding.



Add this article in :

 

There is not yet any comment !



241 Rating: 9.0/10 (1 vote cast)

 

 Members
Log In !
Email Address :

Password :


Change my passowrd !
Sign Up !
We have : 3 members.
Latest member : albans
 
 Last Posts
 
 Advertising
 
©PhpTips.im 2010
Powered by PikaCMS.
Quick Links :
Home | Browse all articles | Downloads | Terms of Use
RSS RSS 2.0 : Articles | Downloads ATOM : Articles | Downloads