# how to write a contact form



## zombienation (Aug 6, 2003)

Hi There I have written a webpage with home, about etc + a contact page

Now I have written a form for the contact button how can i Direct it to go to my email after i press the submit button

hopefully somebody can help me out.


----------



## ehymel (Aug 12, 2007)

Lots of ways to do this on your own, but I have used formmail.php (available here) with success in the past.


----------



## dojo-kim (Nov 28, 2011)

One possibility is to use two separate files:

contact.html

Include this form somewhere in your HTML code.

```
Name:

Subject:

E-Mail:

Website:

Message:
```
mailer.php

Put this file in the same folder as the contact page.

```
// get variables from the post
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$website = $_POST['website'];
$message = $_POST['message'];

// Your email here
$to = "[email protected]";

// The actual mailer function 
mail($to,$subject,$message);
```
This is just a simple form with a POST method. The php then grabs the HTML form data and puts them into the php mail function. If you want to, you should also add some sort of CAPTCHA to prevent from bots/spam entering your email.


----------



## ehymel (Aug 12, 2007)

dojo-kim said:


> One possibility is to use two separate files:


Why 2 separate files? It's much easier from a maintenance standpoint to have all of this in a single file, then have the form page post to itself. After processing, the php code can redirect to another page (e.g., "Thanks for your submission") or flag fields on the form for further information gathering.


----------



## dojo-kim (Nov 28, 2011)

Well yes of course, I simply used two files to point out the way forms work...

contact.php:

(instead of contact.php, you can use <?php echo $_SERVER['PHP_SELF']; ?>)

```
<?php
if(isset($_POST['submit'])){ 
// get variables from the post
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$website = $_POST['website'];
$message = $_POST['message'];

// Your email here
$to = "[email protected]";

// The actual mailer function 
mail($to,$subject,$message);  
}
?>

Name:

Subject:

E-Mail:

Website:

Message:
```
That should do the trick.


----------



## buff3r0vrfl0w (Feb 7, 2011)

zombienation said:


> Hi There I have written a webpage with home, about etc + a contact page
> 
> Now I have written a form for the contact button how can i Direct it to go to my email after i press the submit button
> 
> hopefully somebody can help me out.


Hey idk if you solved this already but I've been playing with google forms and kinda love the simplicity and easy email notification. Here's a simple tut on how to setup a form to dump to a spreadsheet and notify you via email of new entries.






just my .2


----------

