adv banner
Web Programming  Home Web Programming PHP Contact Form
rss

Contact Form

Author: Ethicsdesigns.com More by this author


Contact FormHere I will explain to you how you can create a simple contact form. The form will collect data from the visitor and send it to any email address you please. The only requirement for this tutorial is for you to have .php enabled on your server. If you don't want to read through the whole tutorial and just want the code simply scroll down to the Conclusion.

1. Here's how to check if you have php enabled on your server. If you already know you have php enabled simply skip to step 2.

Open any text editor and type in the following code

<?
phpinfo();
?>

Save it as phpinfo.php and upload it to your server. Now go to the page you just uploaded in your browser.

http://www.yoursite.com/phpinfo.php

If you have PHP installed you will see a huge page with all the details of your PHP installation on it.

2. Now this tutorial consists of 2 files. You will have your form.php, and your processor.php. The form.php will be just as it says, your form. The processor.php on the other hand will be the command that actually sends all the info that one has inserted into the form to your email.

I'm assuming you already have a form built, but if not let's use the one I have on my Contact Us page.

<form name="contact" method="post" action="processor.php">

Name
<br />
<input name="name" type="text" size="31" id="name" />
<br />

Email
<br />
<input name="email" type="text" size="31" id="email" />
<br />

Web site
<br />
<input name="website" type="text" size="31" id="website" />
<br />

Comment
<br />
<textarea name="comment" cols="23" rows="4" id="comment" >
</textarea>
<br />
<br />

<label>
<input type="submit" name="Submit" value="Submit" />
</label>
<br />

</form>

Now if you know any HTML at all this should all look pretty straight forward. If not try to bare with us. Say you wanted to add another field, you may notice that Name, Email & Web site all have identical code, so how do you add another field? That's right just copy and paste one of the fields that are already up there and rename it. For example...

Age
<br />
<input name="website" type="text" size="31" id="website" />
<br />

When your satisfied with all your fields save your form as form.php and move on to the next step.

3. Now for some tricky code. You don't have to edit anything here besides the fields I mention below. So start up a new file and paste the following code into it.

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="Website Message:nnn";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $keynn";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $vnn";
}
} else {
$val = stripslashes($val);
$msg.="$key: $valnn";
}
}
$recipient="your@email.com";
$subject="Website Message";
error_reporting(0);
if (mail($recipient, $subject, $msg)){
header( "Location: http://www.yoursite.com" );
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";
?>

There's only 3 things you should edit in this file.

$recipient="your@email.com"; That line is pretty obvious, put in the email where you'd like your form to be sent to.

$subject="Website Message"; This will determine what the Title of the email will be.

header( "Location: http://www.yoursite.com" );

And last but not least this is where you'd like the person to be taken once they have filled out the form. It's always nice to have some kind of thank you message so keep this in mind when linking this last step.

Conclusion

The complete code incase you don't want to read through all of that, simply edit the sections in orange and you'll be fine.

Form.php

<form name="contact" method="post" action="processor.php">

Name<br />
<input name="name" type="text" size="31" id="name" />
<br />Email<br />
<input name="email" type="text" size="31" id="email" />
<br />Website<br />
<input name="website" type="text" size="31" id="website" />
<br />Comment<br />
<textarea name="comment" cols="23" rows="4" id="comment" >
</textarea><br /><br />
<label>
<input type="submit" name="Submit" value="Submit" />
</label><br />

</form>

Processor.php

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="Website Message:nnn";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $keynn";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $vnn";
}
} else {
$val = stripslashes($val);
$msg.="$key: $valnn";
}
}
$recipient="your@email.com";
$subject="Website Message";
error_reporting(0);
if (mail($recipient, $subject, $msg)){
header( "Location: http://www.yoursite.com" );
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";
?>


Author's URL: www.ethicsdesigns.com

Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Read/Add comments to "Contact Form"

comments  alexaTLV February 21, 2007 says:
Contact Form
This is nice, clean and simple code but for some reason the only thing i get is "An error occurred and the message could not be sent." message. Any ideas why it's not working? My hosting allows php, so what's the problem?!?!