Page 1 of 1

VERY Simple Contact Form How to build an ultra-simple-no-frills contact form Rate Topic: ***** 1 Votes

#1 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 796
  • Joined: 25-February 08

Post icon  Posted 21 May 2008 - 09:29 PM

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:

 
<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:

<?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.

Is This A Good Question/Topic? 2
  • +

Replies To: VERY Simple Contact Form

#2 chrisp200  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 18-November 08

Posted 28 January 2009 - 01:38 AM

having trouble with this email script. Besides changing the email address, should anything else need modified?
Was This Post Helpful? 0
  • +
  • -

#3 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Posted 20 February 2009 - 03:39 AM

:^: Excellent tutorials it really helps me Thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Posted 10 May 2009 - 08:30 AM

The problem with something like this is that you assume the person is using a server that has a POP3 or IMAP server or something set up by default...and that may not always be the case.

Otherwise, it's a pretty good tutorial.
Good job.

(And yes, I realize I'm a bit late, sorry about that.)

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#5 fujiyouriko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 14-July 09

Posted 19 July 2009 - 07:18 PM

how about when there are several email recipients and they are inside a dropdown menu?
Was This Post Helpful? 0
  • +
  • -

#6 ranytan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 09

Posted 24 December 2009 - 09:46 AM

Hello, I used your code.
The email sends fine.
But on step 4, redirecting to thankyou returns error.

I really don't know much about php. I don't know whats wrong. Please help.

http://edumy.co.cc/Contact.html

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

$to = "gettheresults@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: http://edumy.co.cc/Thankyou.html');

?>
</body>
</html> 


Was This Post Helpful? 0
  • +
  • -

#7 ranytan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 09

Posted 25 December 2009 - 07:15 AM

Anyone?
Changing to quote or double quote doesn't work.
Was This Post Helpful? 0
  • +
  • -

#8 ranytan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 09

Posted 28 December 2009 - 01:19 PM

Bump.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1