This is a very simple tutorial, yet work like a charm and does all the things it needs to do. First of all, here's the code:
| <? $subject = $_POST['subject']; // This is will be the subject of the email $email = $_POST['email']; $comments = $_POST['comments']; $submit = $_POST['submit']; if($submit) { // Replace name@host.com with your email address! mail("name@host.com", "$subject", "$email", "$comments"); } ?> |
Save it as contact.php
Lets break it down. The variables gather the information from the forms and make it a cleaner code. The if($submit) tells the mail(); function that it can send the email. It helps to keep it from sending annonymous emails to your email address.
You have to change the name@host.com to your email address to be able to get the email from the user. The rest is fine do not change, unless you need to.
Now for the forms!
| <form method="post" action="contact.php"> Subject:<br /> <input type="text" name="subject" size="20"><br /> Email:<br /><input type="text" name="email" size="20"><br /> Comments:<br /><textarea cols="20" rows="5" name="comment"></textarea><br /> <input type="submit" value=" Submit "> </form> |
which will produce the following
This is really simple, I should not have to explain it. Leave the name fields alone, unless you need to. You can change the size too.
Related Articles
- Simple Contact FormLearn how to write a clean and easy contact form for your site.
- Form ValidationLearn the different ways of validating your form with Javascript.
- Form EmailLearn to send mail with the php mail(); function.
- 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.
- 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.
- Form Validation using PHPToday Ill take it up a level and re-write that script using the if, else conditional and empty() function to include form validation.
- Create User Friendly Confirmation PagesIncluding a form on your website is the best way to get feedback from your visitors. You can have a form to gather your visitors email address so that they can subscribe to your ezine. Or you can have a form to collect visitors comments about your site. Also, forms allow your visitors to send.....
- 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....
- PHP Feedback Form: AdvancedThe problem with my basic PHP Mail Form tutorial is that it offers no protection from spam and meta tag injection. This is fine if you have a very low-traffic site not listed at major search engines, or have a private website, but bigger sites need more.
- Php Mailing List with no MySqlSimple mailing list with admin panel and subscriber unsubscribing function. No MySQL needed.
