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: 91
  • View blog
  • Posts: 798
  • Joined: 25-February 08

Post icon  Posted 21 May 2008 - 09:29 PM

In this tutorial I知 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値l just build a functional form with php integration.

The first thing that we値l 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知 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 堵et method or the 菟ost method. The main difference between the two is that the 堵et method will include the variables in the URL in the address bar, and the 菟ost 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壇 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 = "[email protected]"; // 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(猫ocation: thankyou.php);

?>


Step 1:

Set a few variables and get their values using the $_POST prefix. If you used the 堵et method on your form replace this with $_GET[地ame綻 etc . . .

Step 2:
I know I said that I wasn稚 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 擢rom: Andrew . When you致e received e-mail you致e probably noticed that the sender痴 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痴 mail() function. This is setup in the format mail(to, subject, message, header). Notice that there is no 吐rom parameter. This is why I included the From in the header, as that痴 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痴 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? 3
  • +

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: 127
  • 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: 240
  • 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 = "[email protected]"; // 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