Here 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 /> <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"; ?> |
Related Articles
- How to make a simple form mailer with PHPAs you may be well aware, displaying your e-mail address on your website can lead to e-mail harvesters picking up your address and adding it to hundreds or even thousands of lists used by spammers...
- Form HandlingWe are going to be working with the $_Post or $_Get arrays. They basically tell the form if i needs to post something, or get something. In this case we are going to be showing you the basics of a contact email form, so we will be using the $_Post array.
- PHP Contact FormFirst, lets create the html section. I made a simple form, but it gets the job done. You can customize it if you want, but remember to use the same form variables.
- The Importance of Having a Feedback Form in Your E-commerce WebsiteInternet marketing is a different ball game altogether when compared to traditional marketing. A brick and mortar store employs salespersons to guide and advise its customers. No such equivalent of a salesperson exists for an online store. This could be one of the reasons why people shy away from on...
- 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 dont want to read through the whole tutorial a....
- Form EmailLearn to send mail with the php mail(); function.
- E-mail form using HTML and PHPSometimes its a nice touch to include an e-mail feedback form on your contact page or elsewhere on your web site. Lets create an e-mail form using html and a simple php script to send it along.
- How to Implement the CAPTCHA with PHP and GDThis article is a gas, it will show you How to Implement the CAPTCHA with PHP and GD. All the necessary code is laid out nice and neat by the author who has also provided you with helpful graphics to make it easy to understand.
- Good 24/7 Hosting Customer Support -- Is It Possible?Even if you are an experienced webmaster you will need to contact the support team time from time in order to solve problems which can be either on your side or web hosting company side...
- Domain Names - Free Privacy - How to Protect your Privacy When You Register a Domain NameAre you worried about your privacy online when starting a website? You should be. When you register domain names - free privacy settings from your registrar can protect you from spam, prying eyes, or worse.
