Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,887 Programmers for FREE! Ask your question and get quick answers from experts. There are 2,077 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



VERY Simple Contact Form

 
Reply to this topicStart new topic

> VERY Simple Contact Form, How to build an ultra-simple-no-frills contact form

akozlik
Group Icon



post 21 May, 2008 - 09:29 PM
Post #1


In this tutorial I’m going to teach you how to build a simple contact form using PHP. This post does not cover form verification or headers, but I plan on writing a few tutorials in the future about these topics. For now we’ll just build a functional form with php integration.

The first thing that we’ll need is a simple form. Lets use the following:

CODE

<form action="contact.php" method="post">
     Name: <input name="name" size="50" type="text"> <br />
     E-mail: <input name="email" size="50" type="text"> <br />
     Subject: <input name="subject" size="50" type="text"> <br />
     Message: <br />
     <textarea rows="20" cols="50" name="message"></textarea><br />
     <input value="Submit!" type="submit">
</form>


Simple enough eh?

I’m going to assume that you know the different parts of the form. Just as a refresher know that the action property tells what page to send the browser to to process the form, and the method property tells you how the variables will be sent to the server. You can use either the “get” method or the “post” method. The main difference between the two is that the “get” method will include the variables in the URL in the address bar, and the “post” method will not. GET methods are typically good in inventory systems where you would like a user to be able to bookmark a specific item, while POST methods are better for form usage.

Anyway, back from one of my tangents. Save that form in whatever file you’d like and send it up to your server.

Now start a new text file and go ahead and save it as contact.php. This is going to be the file that processes the form, and mails the message to the user.

First the code, and then the explanation:

CODE

<?php
// Step 1
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = "admin@gmail.com"; // The e-mail address you want the message sent to

// Step 2
$header = "From: " . $name . " < " . $email . ">“;

// Step 3
mail($to, $subject, $message, $header);

// Step 4
header(”Location: thankyou.php”);

?>

Step 1:

Set a few variables and get their values using the $_POST prefix. If you used the “get” method on your form replace this with $_GET[’name’] etc . . .

Step 2:
I know I said that I wasn’t going to put in headers, but I felt this is a pretty good one to include in this tutorial. I go more into headers on a different post, but know that this one is great to have. The header variable winds up with the form “From: Andrew “. When you’ve received e-mail you’ve probably noticed that the sender’s name shows up in the From field in your inbox. This is because of the way the header was set up. When the user sends a message from your page, the name they provided will show up in your inbox.

Step 3:
The meat and potatoes of this project is PHP’s mail() function. This is setup in the format mail(to, subject, message, header). Notice that there is no “from” parameter. This is why I included the From in the header, as that’s how the mail servers read that information. We simply include our variables and let our sendmail server do the rest.

Step 4:
After mail is sent, you can then redirect the user to a thank you page. Remember that this header() function must be used before any text is displayed to the browser. If you use any echo statements or straight HTML before this point, the redirect will not work and you will get an error.

Well, that’s about all I have to say on the topic of simple forms. Hopefully somebody finds this to be a useful article and is able to get some work done thanks to it. As usual comments and questions are welcome.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/13/08 03:16AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month