How to Create Kick-Ass PHP Contact Forms

Posted in Tutorials • Posted on 86 Comments

I am sure that almost everyone can agree on the importance of contact forms for use on everything from static HTML websites, to WordPress powered websites. I found myself many times creating custom PHP contact forms for clients and always changing things around to suit the needs of the client.

After going through this article you should have a better understanding of creating custom PHP contact forms. These can be really useful in your own projects, as well as projects for clients. I have used these for basic contact forms, surveys, and even to create simple help desk ticket systems for clients. The list is endless, just be creative. I will discuss everything that you will need to know to make your own custom HTML and PHP forms.

Update 2013: free contact form in HTML5 and CSS3

Forms

First things first – To create a form in our HTML document, we will need to select the location we will be placing the form. Generally, most forms will start with:

<form>

and end with the closing tag of:

</form>

The form action will tell this form what to look for when the submit button is pressed. In this example we will be working with below, this is a second file that we will be creating called mail.php

The beginning line of our code that begins our form shows our action of mail.php – and the method of POST – which will trigger the php script to send the email when the forms is filled out, and the submit button is pressed.

Action and Method of mail.php

<form action="mail.php" method="POST">

The last thing we will need to understand before starting our form is the use of INPUT – which will tell browsers to allow an input of text type, to complete a field. Using this along with textarea will allow us to create our form and create a space for users to input information that we will later use PHP to send via email. Each one of these areas we create on our form will be given a NAME that we will also be using on our PHP document to mark the information being sent.

Taking a Look at It

Now let’s begin our example. We will create a very simple starting point that I will show you how to modify for your own needs. Understanding the code and how it works will help you use it better and help ensure you have less problems when placing this on a live website.

I will start with a very basic contact form to get us started. Here is the basic HTML that we will use to create our form.

HTML Form Code

<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

Using the code above – You can insert this directly into your html document to create the form itself. Later we will look at modifying this more and creating something a little more custom.

Now for the PHP

Now, to make our form work, we will need to use a little php. This part is actually easier than most people think. We will be using the PHP $_POST funtion, and creating labels for each name that we have created in our form. This will allow us to further customize the form later on as well.

Now we will create our mail.php file – This is what will generate the email from the form and actually mail it:

mail.php

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
 

Notice our three name tags we have created. We have Name, Email, and Message. These are the three that we created in our form. This is the information that will be sent from our contact form via email.

The $recipient area will need to be modified to fit YOUR email address where you wish to have the email sent to. You can also modify the other information as needed such as the subject, and success message. We will get more into these later when we begin customizing the form even more.

Customizing More

Now since we have the basic idea of the html form, and tieing it together with our PHP to create a basic contact form, I will begin to go a step further and now show how you can customize this form even more to fit your needs for your project. I will show how to add a dropdown option box, and explain adding checkboxes or radio buttons for selection items to be chosen, and emailed from the form.

Adding Dropdown Option Boxes

To add a dropdown box we will need to add the section within our HTML code to create the area for the form, as well as add the proper code to our PHP to recognize the input from the HTML, and be able to send it.

Here is a simple example HTML dropdown box:

<p>Dropdown Box</p>
<select name="dropdown" size="1">
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
<option value="Option3">Option3</option>
<option value="Option4">Option4</option>
</select>
<br />

In the example above, we have created a dropdown box with options 1 through 4. The option value will be what is actually submitted, and the Text within the will be what the user actually sees when making a selection. Remember that this will need to be inserted into your html document within the form fields.

Here is an example of the completed HTML form we have created with the dropdown box included:

HTML Form with Dropdown Box

<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">

<p>Dropdown Box</p>
<select name="dropdown" size="1">
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
<option value="Option3">Option3</option>
<option value="Option4">Option4</option>
</select>
<br />

<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

Now we will need to change our PHP to make sure the information from the HTML form is rendered and submitted to the provided email address.

Let’s take a look at our modified PHP that will now have the dropdown box readable.

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $POST['dropdown'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

Notice that we have added “dropdown” as a $_POST variable that will now be sent. The dropdown name itself comes from the html portion that is labeled as

The size option lets you select how many rows will be viewable at one time. The most general setting for this is “1” but you can change it to more if you would like.

Adding Radio Buttons and Checkboxes

TO add Radio Buttons and Checkboxes the same will apply as the above. We will need to add it within our HTML code, and then modify the PHP to take the input from the HTML form and properly send it.

Here is an example of the HTML code for adding Checkboxes:

<p>Request Phone Call:</p>
Yes:<input type="checkbox" value="Yes" name="call"><br />
No:<input type="checkbox" value="No" name="call"><br />

Full Example Using All Elements

For this example I have changed some of the names to we can create a custom contact form for our completed example now that we have a basic understanding of the way it works.

Our HTML Form

<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">

<p>Request Phone Call:</p>
Yes:<input type="checkbox" value="Yes" name="call"><br />
No:<input type="checkbox" value="No" name="call"><br />

<p>Website</p> <input type="text" name="website">

<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
<option value="Emergency">Emergency</option>
</select>
<br />

<p>Type</p>
<select name="type" size="1">
<option value="update">Website Update</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">New Products</option>
</select>
<br />

<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

And again, our PHP that will correspond with this HTML form to make it work:

Our completed PHP

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "[email protected]";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

Customizing the Thank you Message on Form Success

Now for the final part of this tutorial I will explain how to customize the very last line of our PHP script we have created. The basic way will just echo “Thank You” on our screen, but we need to make a better way so our viewers can easily have a way to get back to another page. This will be useful in creating a custom page redirect, or a link to bring the user to a different area after completing the form. Remember that when working with PHP, some of the HTML will be different as to not disrupt our PHP code.

We will need to use single quotes ‘ instead of double quotes ” within this one, so we don’t end our php arg.

We will be adding a space after the “thank you” message, and adding a link back to our “form.html” document (Or whatever link you wish to create) – and also changing the color of the link using inline styles.

Let’s take a look at the modified echo command in our mail.php file:

echo "Thank You!" . " -" . "<a href='form.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";

You can play around with the example above to create your own thank you message for your site. Inline styles are not required, I just used them for this example instead of including a stylesheet. Remember that the echo command is only seen on a successful send of the message. Otherwise, the error message is sent.

Download The Files

I am providing the download for the completed form for you to play with. Feel free to use it any way you wish, and customize it for your own projects. There are still many other things that can be done with PHP for your contact forms. One that you might want to consider is CAPTCHA, which prevents spam email. You can also customize the other portions of the form and create your own! Have fun, and I hope that everyone has enjoyed the article and finds it useful for their own needs. You can download the example files by clicking [HERE]

Note: I have included a few styling examples using CSS in the demo download. This will allow you to see the forms styled and understand how to style them using CSS.

10 Written Articles

My name is Kevin. I am a 30 year old freelance web designer. I have been working with HTML, CSS, and PHP for 6+ years - And creating websites using Wordpress for 3 years. I enjoy creating websites and also doing some graphic design using Photoshop and Illustrator. I hope everyone enjoys reading my articles and I look forward to your questions, comments, and feedback.

86 Comments Best Comments First
  • geoff

    Wednesday, November 9th, 2011 23:56

    69

    I get syntax errors for the $recipient portion. How do I make it error-free?

    +33
  • PDC

    Sunday, October 10th, 2010 21:41

    38

    Kevin, great tutorial, and I apologize for all the disparaging comments made by other people. Why they are wasting their time complaining to you about validation and sanitation, I don’t know, if they know enough code to get through this tut then they should be able to tackle those issues by themselves. This isn’t an advanced tutorial (but there isn’t anything wrong with that), and there is a lack of good php form tutorials out there so I’m really thankful you had the patience to put this one together. I just wanted to say thanks a bunch, and keep of the great work!

    +23
  • giedrius majauskas

    Monday, September 27th, 2010 12:13

    7

    I think this is the best way to get your website in multiple black lists.
    Why? because this form is superb to spammers. It performs no data sanitization,
    And spammers can construct additional headers into message.
    This allows them to spam other people USING your webserver without you even knowing it.

    +7
  • Christoph

    Tuesday, September 28th, 2010 10:11

    23

    I find this article very useful for learning the basic techniques behind a php contact form (as mentioned before with the need to add certain features, like validation, spam-protection. etc.). What bothers me is the article’s title… today, everything is “kick-ass”, “awesome”, and “breath-taking”. Of course you get the people’s attention with such titles, but sorry, this is a really nice basic tutorial, but it’s not “kick-ass” :)…

    +3
    • Kevin Stanley

      Thursday, September 30th, 2010 13:02

      29

      Don’t hold me responsible for the title. It was posted that way, and I just wrote the tutorial :)

      Validation is a must, as well as spam protection. This tutorial was meant to show the use of HTML forms and PHP.

      Thanks for the comment!

      +3
  • Gennady

    Wednesday, February 22nd, 2012 20:26

    77

    ALWAYS, I mean ALWAYS, literally *always* sanitize user input. If somebody decides to store this in any other way besides sending via e-mail (@ESTEBAN pointed out correctly, that wp_mail() is available), or echo out the thank you message with the $name in it, XSS will become an immensely huge problem. Validation and sanitization of user input should ALWAYS be touched upon when dealing with the topics of forms. Besides, WordPress has great functions such as is_email(), esc_attr(), get_the_content() even, that can be used to sanitize and provide a more secure environment, without rolling their own functions.

    Safe!

    +2
  • Badr

    Monday, December 5th, 2011 22:59

    70

    Awesome work.
    Very lucid.

    +2
  • Darin

    Wednesday, March 14th, 2012 22:25

    81

    Hey, great tutorial. However, I have a problem: When I try to submit the form, instead of emailing me and providing the thank you message, it downloads the php page. When I run the .html page in my browser, it shows up fine but when I click send it downloads mail.php, instead of working.
    Any suggestions?

    +2
  • Matrox

    Saturday, April 28th, 2012 12:45

    85

    Awesome tutorial, thanks!
    Also, if you want to redirect user who send email, simply put this code in mail.php just behind “$mailheader” line:

    header("Location: email-send.php");

    This will redirect user to page “email-send.php”, where you can put your “thank you page” ;)

    +2
  • phil

    Friday, March 2nd, 2012 11:49

    78

    Hi how would you change the script to include a copy of the form being sent to the person who filled in the form?????????

    Thanks

    +2
  • Eric

    Friday, March 23rd, 2012 04:35

    83

    Thank you for sharing this!!! It really helped me out. I did however run into a problem. My form has about 15 fields and 5 of them are drop down menus. Well after I loaded the files to my server all the drop downs went missing. If i hover over where they use to be and click the drop down menu re-appears. How would I make them reappear permanently? Thanks again!

    Eric

    +2
  • Nils

    Saturday, March 17th, 2012 08:32

    82

    Thanks for posting this. It helped with a client’s site. You rock!

    +2
  • Brad Billman

    Friday, October 1st, 2010 19:14

    33

    You also want to add http://www.google.com/recaptcha a free captcha so that bots can’t just spam flood your email.

    +1
  • rahul

    Thursday, January 26th, 2012 11:43

    75

    hey i want to know that where to upload my swf file ,php and logo for contact form active on my blog

    +1
  • jack

    Wednesday, February 8th, 2012 03:37

    76

    This is GREAT!!! THANK YOU THANK YOU!!!!!!!!!!!!!!!!

    +1
  • pesho

    Monday, September 27th, 2010 14:31

    14

    Nice
    awesome Tutorial
    thanks

    +1
  • sharmeen

    Monday, March 26th, 2012 13:36

    84

    Hello,
    To contact an advert owner, the users will have to fill a contact form which will be send to the owner email(hotmail,yahoo,…), but the php script is hardcoded so how i will be able to do it?is it possible to write a script which can be use for all the contact form without modifying it each time a user is contacting different Ads owner?please can anybody help me out!!!…

    Thank you.

    +1
  • Brett Widmann

    Thursday, January 27th, 2011 06:01

    46

    This was a really helpful tutorial. Thanks for the download as well!

    +1
    • vilas patil

      Friday, May 6th, 2011 12:30

      49

      thank you

      +1
  • Starry

    Wednesday, January 25th, 2012 22:07

    74

    I must be totally stupid I can’t figure this out.. is there anyone that does php scripting that can tell me what I’m doing wrong? Maybe my form is too darn complex.. imagine that LOL

    If anyone has any insight to help or if Kevin will email me that’d be friggin awesome!!

    +1
  • philip

    Monday, December 19th, 2011 15:05

    71

    Hi excellent tutorial, however what if they have what multiple option selection for radio button or check box how do i read in all the values, give each radio box a unique name?, but what if not all radio buttons are checked.

    thanks man,

    Philio

    +1
  • wqs

    Monday, September 27th, 2010 21:25

    22

    cool yaar…very good looking form

    +1
  • Attila

    Friday, September 2nd, 2011 22:18

    61

    For some reason, when I used this code on m website, the form doesn’t actually make it to my email. ive changed the email address in my mail.php. but it Wont work!. im not sure what to do right now.

    +1
  • Katieful

    Friday, October 1st, 2010 03:16

    30

    I tried this on my portfolio and when I press submit, I get a blank white page and no email. Do you know what could be the problem? I only have text fields, and I put the following in a separate document called mail.php:

    Is it because I’m testing it on a local folder and it needs to be on a server to work?

    0
    • Brad Billman

      Friday, October 1st, 2010 19:08

      31

      It definitely needs to be on a server. Or at least something with php installed. You could use WAMP on your windows machine to run php scripts.

      -2
      • Katie

        Monday, October 4th, 2010 01:46

        35

        Yeah, I thought that was it. Thanks!

        0
    • Kevin Stanley

      Saturday, October 2nd, 2010 04:32

      34

      PHP needs to be ran online. Unless you are set up for it locally, it will only work when placed online.

      +3
  • meo;

    Thursday, September 30th, 2010 14:59

    27

    Great tutorial for a newbie like me.

    Commenters highlight that validation, verification and sanitation is absolutely necessary – please could they post links to their tutorials/trusted tutorials detailing the steps?

    0
  • Geoffrey Gordon

    Monday, September 27th, 2010 15:13

    15

    Great tutorial, how do you connect it to mysql database or is it unnecessary? Excuse my ignorance, I use jotforms, I code a lot in html and css, but learning php, so I want to make the transition and code my own forms.

    0
    • Kevin Stanley

      Monday, September 27th, 2010 19:30

      12

      No MySql needed. Just the HTML form and PHP. As from the other comments, you can see that you still need some form of validation. This will be super easy to add into the code before placing it on a live site.

      0
  • Stacy

    Tuesday, September 28th, 2010 11:15

    25

    Thanks for another really useful tutorial! Just one query – how do you make the input fields mandatory? I know this is possible but can’t find out how to do it.

    0
    • Brad Billman

      Friday, October 1st, 2010 19:12

      32

      Javascript will be the “prettiest” way to make them required but people can disable javascript so you can just do a check in php like if(!$_POST['example']) or if(!isset($_POST['example']) and if either of these return true then do not finish the submission and return and error like “example is a required field” I suggest repopulating all of the boxes that they did fill in so they don’t have to.

      0
    • Kevin Stanley

      Thursday, September 30th, 2010 13:02

      26

      There is a really good and very simple way using JavaScript to make fields mandatory. Search for it online and I am sure you will find it.

      0
  • Jimmy

    Monday, September 27th, 2010 15:29

    6

    Hi,
    It is imperative to sanitize all input data.

    It can be used to evil purposes just by changing the email input to a textarea and typing
    [email protected] \nBcc: [email protected], [email protected], [email protected], …, [email protected]

    Nice blue form tho.

    0
    • Brian Cline

      Wednesday, October 6th, 2010 05:36

      37

      Sanitizing the email field and validating the fields are filled was the first thought I had when I finished the email.

      I believe we are doing any designers or other person building a website a strong disservice when we include code that is said to do exactly what the user needs but includes absolutely no security.

      0
  • Jean-Baptiste

    Monday, September 27th, 2010 15:11

    5

    What about “labels” before the “input” instead of “p” ?
    With of course “for” attributes relative to input’s “id” ?

    Whatever, thanks for the tutorial !

    0
  • Wong

    Monday, September 27th, 2010 14:52

    4

    Love it! But can add in a hidden form and check the form for spam too?

    0
  • Kevin Stanley

    Monday, September 27th, 2010 13:37

    2

    I agree that there is much more before putting this as is on a live site. The purpose of this tutorial is to show how to get html forms to interact with PHP to perform functions and calls. With validating this form, and adding a captcha, it would be ready. I did add at the footnotes that more modification could be done, and wanted to use the tutorial to let people learn how to build the form itself, while learning to work with forms and PHP. Thank you for the feedback.

    0
  • Irfan Suleman

    Monday, September 27th, 2010 14:33

    3

    good, keep it up

    0
  • Ramona

    Monday, September 27th, 2010 12:36

    8

    I’ve never been able to actually make a good form. This tutorial is indeed priceless. Will surely bookmark and promote it myself too.

    0
  • Suzy

    Monday, September 27th, 2010 12:40

    9

    I like the idea, but its very basic :D i missed the validation part, but great tut for html beginners! Keep on the good work!

    0
  • Sam

    Monday, September 27th, 2010 18:26

    19

    Kevin, when using a wordpress install do you prefer raw code forms or using a plugin like contact form 7?

    0
    • Kevin Stanley

      Monday, September 27th, 2010 19:27

      11

      Contact Form 7 is excellent!

      0
  • Stacy

    Monday, September 27th, 2010 18:52

    20

    Hi – thanks for another excellent tutorial! One question – I know its possible to make input fields mandatory but can’t work out how to do so – can anyone help?

    0
  • Harsh Agrawal

    Monday, September 27th, 2010 22:23

    18

    Awesome tutorial.. Thanks :)

    0
  • Scorpiono

    Monday, September 27th, 2010 21:13

    16

    Shouldn’t we have a verification if fields are empty or not and if $_POST['submit'] isset? I’m not a PHP developer, I’m just asking.

    0
    • Brian Cline

      Wednesday, October 6th, 2010 05:31

      36

      I would recommend you always validate any forms using javascript as a first line of defence, and then revalidate on the server. It is also wise to validate that the fields (especially email fields) don’t contain any nasty surprises.

      Whether you use a captcha or some other form of antispam is up to you, but I would also recommend this.

      0
  • Suzanne

    Saturday, November 6th, 2010 17:47

    43

    Sorry … Freudian slip … I meant GOOD validation :)

    0
  • Cee

    Saturday, June 25th, 2011 23:30

    56

    Wow, this is a really good tutorial for noobs like me. It’s really well-explained and to the point, thank you so much! You’re a life-saver :)

    0
  • Tina

    Tuesday, September 20th, 2011 19:24

    62

    Awesome tut! I just did this last week with the help of a co-worker with a lot more HTML and PHP experience than myself. He did exactly what you have here, but you have covered some things he didn’t. This will be a page I visit OFTEN for future work to include those extra items. Great work and thank you very much for your time.

    0
  • Stephen

    Thursday, September 22nd, 2011 13:30

    63

    Great tutorial! It did take me longer than it should! Getting to grips with this stuff now.

    0
  • Leacher

    Sunday, August 21st, 2011 14:53

    60

    is there any way to add referral url? I want to put it to my Interspire Shopping Cart 6 which does not have this feature.

    0
  • Efe

    Friday, August 12th, 2011 01:23

    59

    Outstanding, easy to follow tutorial. Many, many thanks for your clear, concise help!

    0
  • Bijayswain

    Friday, October 29th, 2010 09:51

    39

    Great Information. I needed this badly.

    Thank U Very Much

    0
  • Ronny

    Monday, September 26th, 2011 23:54

    64

    Hi there:
    Great tutorials. I am however, trying to figure out how to make the drop down menu allow for multiple selections. Can you please show any code on how to do that?
    Thanks again,
    Ronny

    0
  • Simon

    Tuesday, October 11th, 2011 16:44

    65

    Thanks very much.
    A very interesting and informative read.
    I need to implement something like this on my website as well.

    0
  • Dan

    Monday, March 5th, 2012 19:39

    80

    I can’t get the drop down menus to show correctly in the email body. The new line seems to be ignored and they just appear in one long line, any ideas?

    0
  • Katie Colton

    Monday, May 14th, 2012 19:01

    86

    I am so glad you created this help file! You really helped me to resolve some issues I had on getting my form to work for my web design class! Thank you! Only one question I have is with checkboxes: When all 6 boxes are selected, I only get the last value, not all of them. I used $interested_in = $_POST['interested_in'];
    Is there some other consideration when multiple values can be selected? How do I make sure all values are successfully transmitted? Thanks.
    -Katie

    0
  • anbazhagan

    Monday, March 5th, 2012 09:39

    79

    This tutorial is very useful for me

    0
  • Erik

    Wednesday, January 18th, 2012 21:25

    72

    This has been a great tutorial, but it seems that I am having trouble actually receiving emails for the form. Additionally the “Thank You” and page redirect are not working.

    Thanks for any help you can offer.

    0
  • Marjorie

    Wednesday, October 12th, 2011 21:19

    66

    Huge headache saver. I’ve been looking for a basic form tutorial like this for a while. Thanks so much, I appreciate it!

    0
  • Saket Jajodia

    Friday, October 14th, 2011 17:58

    67

    Do we don’t need to add server/host name, ID and password??

    0
  • Katie

    Thursday, October 20th, 2011 22:40

    68

    I have struggled with forms/php for a long time now. I used this and was up and running in minutes. Thank you SO much for your clear explanation.

    0
  • paresh

    Thursday, July 28th, 2011 21:59

    58

    i keep gettin error when i upload the form on my website. y is that so? can anyone help me out pls..

    0
  • Bijayswain

    Saturday, October 30th, 2010 03:40

    40

    How to make some fields mandatory ? Can any one give code to do this in the above example ?

    0
  • Bijayswain

    Saturday, October 30th, 2010 03:43

    41

    we have a Local webserver on our interanet using windows2003 server.
    and also have a local domino mail server in our interanet. can i use this code to create a form on our local webserver and send forms via local mail server. what are the requirements and how to do it ?

    0
  • Fathernugen

    Thursday, January 13th, 2011 15:15

    45

    I’ve found that they are line breaks so the output of the field become separated in the email received.

    Now I’ve another problem…. The form sends and I’m taken to a new page where it just ehoes thank you and provides a link to a page of my choosing. How do I get the form to send me to a page with the same template as my site?

    Anyone……..????

    0
  • Fathernugen

    Thursday, January 13th, 2011 14:06

    44

    Nice tutorial, just what I needed. I found a form validation jquery code, but lacked a mail.php file. Every other tutorial assumes a level of knowledge I do not possess. . .

    One question….

    What are these bits in the mail.php code?

    rn

    Do I need these?

    0
  • Suzanne

    Saturday, November 6th, 2010 17:46

    42

    Any recommendations on god validation? The tutorial is great, but having all info in one place would be even better! thanks :)

    0
  • Dave Mellett

    Monday, September 27th, 2010 12:58

    1

    Nice, clear explanation of how a php contact form works. Hardly kick-ass though, this would have to be seriously upgraded for production.

    0
  • Joshua

    Friday, March 18th, 2011 21:43

    47

    I keep getting a internal server error.. even when i upload unchanged form.htm and mail.php(exept recipient mail adress) can someone help me out please? just can get it to work. i want to use it on a iweb created website.

    0
  • NAT

    Tuesday, May 17th, 2011 19:41

    51

    Thanks for the great tutorial. Quick question: (I’m clearly missing something here!)
    I’m using multiple checkboxes in my html form as follows:

    SMS/text subscriptions
    Mobile websites
    Mobile applications
    Not sure, tell me my options

    These are intended to be a multi-checked scenario, and I’d like all values checked to return to me. Problem is that only one of the ckecked items returns. Any ideas on how to alter this to make sure I get all the data returned that is checked?

    Thanks in advance…

    0
    • Rean John Uehara

      Tuesday, May 17th, 2011 20:19

      52

      Hi, are you using arrays?

      0
      • NAT

        Wednesday, May 18th, 2011 17:57

        53

        No. I’m not sure how to write that, being a beginner… Here’s what I’m using: (formats being the name of the checkboxes)

        $formats = $_POST['formats'];
        $formcontent="formats: $formats \n"

        0
        • Rean John Uehara

          Wednesday, May 18th, 2011 18:56

          54

          Try doing this on your form:
          input type=”checkbox” name=”formats[]” value=”1″/>
          input type=”checkbox” name=”formats[]” value=”2″/>
          [ and ] are important
          and so on..

          then in your php send the data by:

          for($i = 0; $i<count($_POST["formats"]); $i++)
          {
          echo $_POST["formats"][$i];
          }

          it should send and display multiple values selected on the form(display values to check).. at least I hope..haven’t had the chance to test this, just wrote it and it’s been a while since I touched PHP. Please tell me it works. lol

          0
          • NAT

            Wednesday, May 18th, 2011 20:11

            55

            Thanks for your help!
            Unfortunately, all I’m seeing is the letter “m” and not the values in the array. My goal is for all the form data to return to me in an email, in case it wasn’t as clear before:)

            $formats = $_POST['formats'];//first set of checkboxes
            for($i = 0; $i<count($_POST["formats"]); $i++)
            {
            echo $_POST["formats"][$i];
            }

            $formcontent="formats: $formats n";
            $recipient ="[email protected]";
            mail($recipient, $formcontent);
            header('Location: thank_you.html');

            -2
  • Chandra

    Saturday, May 14th, 2011 17:05

    50

    THANK YOU!!! Well written, easy to follow, and best of all to the point. Thank you.

    -1
  • Peter

    Monday, September 27th, 2010 15:31

    17

    What not validation? If someone is using this turorial because they can’t code a form they are not going to know how to validate it with php….

    -1
    • Kevin Stanley

      Monday, September 27th, 2010 19:29

      21

      There are so many different ways to validate. This tutorial is for showing how you can make html forms work with PHP to perform a funciton. Validation should always be done I agree, but the choice is up to the user creating the form. Having a good understanding of how the forms and code works will also better help someone trying to learn to code themselves, and how to implement a CAPTCHA if they choose.

      +3
  • ESTEBAN

    Tuesday, January 24th, 2012 21:20

    73

    Why not use wp_mail() ?

    -2
  • re1jo

    Tuesday, September 28th, 2010 11:12

    24

    Unsafe code, don’t use straight without input sanitation.

    -2
    • Kevin Stanley

      Thursday, September 30th, 2010 13:00

      28

      I have mentioned that already. This tutorial is to show the use of HTML forms and PHP. The code should not be used without some form of sanitation.

      +3
  • roxx

    Monday, September 27th, 2010 13:15

    10

    it is better to sanitize user inputs before sending emails, inserting into db etc..

    -3
  • Nicolas

    Monday, September 27th, 2010 13:51

    13

    You forgot validation. Isn’t your code vulnerable to “email header injection”?

    -4
  • Katie Colton

    Monday, May 14th, 2012 19:01

    86

    I am so glad you created this help file! You really helped me to resolve some issues I had on getting my form to work for my web design class! Thank you! Only one question I have is with checkboxes: When all 6 boxes are selected, I only get the last value, not all of them. I used $interested_in = $_POST['interested_in'];
    Is there some other consideration when multiple values can be selected? How do I make sure all values are successfully transmitted? Thanks.
    -Katie

    0
  • Matrox

    Saturday, April 28th, 2012 12:45

    85

    Awesome tutorial, thanks!
    Also, if you want to redirect user who send email, simply put this code in mail.php just behind “$mailheader” line:

    header("Location: email-send.php");

    This will redirect user to page “email-send.php”, where you can put your “thank you page” ;)

    +2
  • sharmeen

    Monday, March 26th, 2012 13:36

    84

    Hello,
    To contact an advert owner, the users will have to fill a contact form which will be send to the owner email(hotmail,yahoo,…), but the php script is hardcoded so how i will be able to do it?is it possible to write a script which can be use for all the contact form without modifying it each time a user is contacting different Ads owner?please can anybody help me out!!!…

    Thank you.

    +1
  • Eric

    Friday, March 23rd, 2012 04:35

    83

    Thank you for sharing this!!! It really helped me out. I did however run into a problem. My form has about 15 fields and 5 of them are drop down menus. Well after I loaded the files to my server all the drop downs went missing. If i hover over where they use to be and click the drop down menu re-appears. How would I make them reappear permanently? Thanks again!

    Eric

    +2
  • Nils

    Saturday, March 17th, 2012 08:32

    82

    Thanks for posting this. It helped with a client’s site. You rock!

    +2
  • Darin

    Wednesday, March 14th, 2012 22:25

    81

    Hey, great tutorial. However, I have a problem: When I try to submit the form, instead of emailing me and providing the thank you message, it downloads the php page. When I run the .html page in my browser, it shows up fine but when I click send it downloads mail.php, instead of working.
    Any suggestions?

    +2
  • Dan

    Monday, March 5th, 2012 19:39

    80

    I can’t get the drop down menus to show correctly in the email body. The new line seems to be ignored and they just appear in one long line, any ideas?

    0
  • anbazhagan

    Monday, March 5th, 2012 09:39

    79

    This tutorial is very useful for me

    0
  • phil

    Friday, March 2nd, 2012 11:49

    78

    Hi how would you change the script to include a copy of the form being sent to the person who filled in the form?????????

    Thanks

    +2
  • Gennady

    Wednesday, February 22nd, 2012 20:26

    77

    ALWAYS, I mean ALWAYS, literally *always* sanitize user input. If somebody decides to store this in any other way besides sending via e-mail (@ESTEBAN pointed out correctly, that wp_mail() is available), or echo out the thank you message with the $name in it, XSS will become an immensely huge problem. Validation and sanitization of user input should ALWAYS be touched upon when dealing with the topics of forms. Besides, WordPress has great functions such as is_email(), esc_attr(), get_the_content() even, that can be used to sanitize and provide a more secure environment, without rolling their own functions.

    Safe!

    +2
  • jack

    Wednesday, February 8th, 2012 03:37

    76

    This is GREAT!!! THANK YOU THANK YOU!!!!!!!!!!!!!!!!

    +1
  • rahul

    Thursday, January 26th, 2012 11:43

    75

    hey i want to know that where to upload my swf file ,php and logo for contact form active on my blog

    +1
  • Starry

    Wednesday, January 25th, 2012 22:07

    74

    I must be totally stupid I can’t figure this out.. is there anyone that does php scripting that can tell me what I’m doing wrong? Maybe my form is too darn complex.. imagine that LOL

    If anyone has any insight to help or if Kevin will email me that’d be friggin awesome!!

    +1
  • ESTEBAN

    Tuesday, January 24th, 2012 21:20

    73

    Why not use wp_mail() ?

    -2
  • Erik

    Wednesday, January 18th, 2012 21:25

    72

    This has been a great tutorial, but it seems that I am having trouble actually receiving emails for the form. Additionally the “Thank You” and page redirect are not working.

    Thanks for any help you can offer.

    0
  • philip

    Monday, December 19th, 2011 15:05

    71

    Hi excellent tutorial, however what if they have what multiple option selection for radio button or check box how do i read in all the values, give each radio box a unique name?, but what if not all radio buttons are checked.

    thanks man,

    Philio

    +1
  • Badr

    Monday, December 5th, 2011 22:59

    70

    Awesome work.
    Very lucid.

    +2
  • geoff

    Wednesday, November 9th, 2011 23:56

    69

    I get syntax errors for the $recipient portion. How do I make it error-free?

    +33
  • Katie

    Thursday, October 20th, 2011 22:40

    68

    I have struggled with forms/php for a long time now. I used this and was up and running in minutes. Thank you SO much for your clear explanation.

    0
  • Saket Jajodia

    Friday, October 14th, 2011 17:58

    67

    Do we don’t need to add server/host name, ID and password??

    0
  • Marjorie

    Wednesday, October 12th, 2011 21:19

    66

    Huge headache saver. I’ve been looking for a basic form tutorial like this for a while. Thanks so much, I appreciate it!

    0
  • Simon

    Tuesday, October 11th, 2011 16:44

    65

    Thanks very much.
    A very interesting and informative read.
    I need to implement something like this on my website as well.

    0
  • Ronny

    Monday, September 26th, 2011 23:54

    64

    Hi there:
    Great tutorials. I am however, trying to figure out how to make the drop down menu allow for multiple selections. Can you please show any code on how to do that?
    Thanks again,
    Ronny

    0
  • Stephen

    Thursday, September 22nd, 2011 13:30

    63

    Great tutorial! It did take me longer than it should! Getting to grips with this stuff now.

    0
  • Tina

    Tuesday, September 20th, 2011 19:24

    62

    Awesome tut! I just did this last week with the help of a co-worker with a lot more HTML and PHP experience than myself. He did exactly what you have here, but you have covered some things he didn’t. This will be a page I visit OFTEN for future work to include those extra items. Great work and thank you very much for your time.

    0
  • Attila

    Friday, September 2nd, 2011 22:18

    61

    For some reason, when I used this code on m website, the form doesn’t actually make it to my email. ive changed the email address in my mail.php. but it Wont work!. im not sure what to do right now.

    +1
  • Leacher

    Sunday, August 21st, 2011 14:53

    60

    is there any way to add referral url? I want to put it to my Interspire Shopping Cart 6 which does not have this feature.

    0
  • Efe

    Friday, August 12th, 2011 01:23

    59

    Outstanding, easy to follow tutorial. Many, many thanks for your clear, concise help!

    0
  • paresh

    Thursday, July 28th, 2011 21:59

    58

    i keep gettin error when i upload the form on my website. y is that so? can anyone help me out pls..

    0
  • Cee

    Saturday, June 25th, 2011 23:30

    56

    Wow, this is a really good tutorial for noobs like me. It’s really well-explained and to the point, thank you so much! You’re a life-saver :)

    0
  • NAT

    Tuesday, May 17th, 2011 19:41

    51

    Thanks for the great tutorial. Quick question: (I’m clearly missing something here!)
    I’m using multiple checkboxes in my html form as follows:

    SMS/text subscriptions
    Mobile websites
    Mobile applications
    Not sure, tell me my options

    These are intended to be a multi-checked scenario, and I’d like all values checked to return to me. Problem is that only one of the ckecked items returns. Any ideas on how to alter this to make sure I get all the data returned that is checked?

    Thanks in advance…

    0
    • Rean John Uehara

      Tuesday, May 17th, 2011 20:19

      52

      Hi, are you using arrays?

      0
      • NAT

        Wednesday, May 18th, 2011 17:57

        53

        No. I’m not sure how to write that, being a beginner… Here’s what I’m using: (formats being the name of the checkboxes)

        $formats = $_POST['formats'];
        $formcontent="formats: $formats \n"

        0
        • Rean John Uehara

          Wednesday, May 18th, 2011 18:56

          54

          Try doing this on your form:
          input type=”checkbox” name=”formats[]” value=”1″/>
          input type=”checkbox” name=”formats[]” value=”2″/>
          [ and ] are important
          and so on..

          then in your php send the data by:

          for($i = 0; $i<count($_POST["formats"]); $i++)
          {
          echo $_POST["formats"][$i];
          }

          it should send and display multiple values selected on the form(display values to check).. at least I hope..haven’t had the chance to test this, just wrote it and it’s been a while since I touched PHP. Please tell me it works. lol

          0
          • NAT

            Wednesday, May 18th, 2011 20:11

            55

            Thanks for your help!
            Unfortunately, all I’m seeing is the letter “m” and not the values in the array. My goal is for all the form data to return to me in an email, in case it wasn’t as clear before:)

            $formats = $_POST['formats'];//first set of checkboxes
            for($i = 0; $i<count($_POST["formats"]); $i++)
            {
            echo $_POST["formats"][$i];
            }

            $formcontent="formats: $formats n";
            $recipient ="[email protected]";
            mail($recipient, $formcontent);
            header('Location: thank_you.html');

            -2
  • Chandra

    Saturday, May 14th, 2011 17:05

    50

    THANK YOU!!! Well written, easy to follow, and best of all to the point. Thank you.

    -1
  • Joshua

    Friday, March 18th, 2011 21:43

    47

    I keep getting a internal server error.. even when i upload unchanged form.htm and mail.php(exept recipient mail adress) can someone help me out please? just can get it to work. i want to use it on a iweb created website.

    0
  • Brett Widmann

    Thursday, January 27th, 2011 06:01

    46

    This was a really helpful tutorial. Thanks for the download as well!

    +1
    • vilas patil

      Friday, May 6th, 2011 12:30

      49

      thank you

      +1
  • Fathernugen

    Thursday, January 13th, 2011 15:15

    45

    I’ve found that they are line breaks so the output of the field become separated in the email received.

    Now I’ve another problem…. The form sends and I’m taken to a new page where it just ehoes thank you and provides a link to a page of my choosing. How do I get the form to send me to a page with the same template as my site?

    Anyone……..????

    0
  • Fathernugen

    Thursday, January 13th, 2011 14:06

    44

    Nice tutorial, just what I needed. I found a form validation jquery code, but lacked a mail.php file. Every other tutorial assumes a level of knowledge I do not possess. . .

    One question….

    What are these bits in the mail.php code?

    rn

    Do I need these?

    0
  • Suzanne

    Saturday, November 6th, 2010 17:47

    43

    Sorry … Freudian slip … I meant GOOD validation :)

    0
  • Suzanne

    Saturday, November 6th, 2010 17:46

    42

    Any recommendations on god validation? The tutorial is great, but having all info in one place would be even better! thanks :)

    0
  • Bijayswain

    Saturday, October 30th, 2010 03:43

    41

    we have a Local webserver on our interanet using windows2003 server.
    and also have a local domino mail server in our interanet. can i use this code to create a form on our local webserver and send forms via local mail server. what are the requirements and how to do it ?

    0
  • Bijayswain

    Saturday, October 30th, 2010 03:40

    40

    How to make some fields mandatory ? Can any one give code to do this in the above example ?

    0
  • Bijayswain

    Friday, October 29th, 2010 09:51

    39

    Great Information. I needed this badly.

    Thank U Very Much

    0
  • PDC

    Sunday, October 10th, 2010 21:41

    38

    Kevin, great tutorial, and I apologize for all the disparaging comments made by other people. Why they are wasting their time complaining to you about validation and sanitation, I don’t know, if they know enough code to get through this tut then they should be able to tackle those issues by themselves. This isn’t an advanced tutorial (but there isn’t anything wrong with that), and there is a lack of good php form tutorials out there so I’m really thankful you had the patience to put this one together. I just wanted to say thanks a bunch, and keep of the great work!

    +23
  • Brad Billman

    Friday, October 1st, 2010 19:14

    33

    You also want to add http://www.google.com/recaptcha a free captcha so that bots can’t just spam flood your email.

    +1
  • Katieful

    Friday, October 1st, 2010 03:16

    30

    I tried this on my portfolio and when I press submit, I get a blank white page and no email. Do you know what could be the problem? I only have text fields, and I put the following in a separate document called mail.php:

    Is it because I’m testing it on a local folder and it needs to be on a server to work?

    0
    • Brad Billman

      Friday, October 1st, 2010 19:08

      31

      It definitely needs to be on a server. Or at least something with php installed. You could use WAMP on your windows machine to run php scripts.

      -2
      • Katie

        Monday, October 4th, 2010 01:46

        35

        Yeah, I thought that was it. Thanks!

        0
    • Kevin Stanley

      Saturday, October 2nd, 2010 04:32

      34

      PHP needs to be ran online. Unless you are set up for it locally, it will only work when placed online.

      +3
  • meo;

    Thursday, September 30th, 2010 14:59

    27

    Great tutorial for a newbie like me.

    Commenters highlight that validation, verification and sanitation is absolutely necessary – please could they post links to their tutorials/trusted tutorials detailing the steps?

    0
  • Stacy

    Tuesday, September 28th, 2010 11:15

    25

    Thanks for another really useful tutorial! Just one query – how do you make the input fields mandatory? I know this is possible but can’t find out how to do it.

    0
    • Kevin Stanley

      Thursday, September 30th, 2010 13:02

      26

      There is a really good and very simple way using JavaScript to make fields mandatory. Search for it online and I am sure you will find it.

      0
    • Brad Billman

      Friday, October 1st, 2010 19:12

      32

      Javascript will be the “prettiest” way to make them required but people can disable javascript so you can just do a check in php like if(!$_POST['example']) or if(!isset($_POST['example']) and if either of these return true then do not finish the submission and return and error like “example is a required field” I suggest repopulating all of the boxes that they did fill in so they don’t have to.

      0
  • re1jo

    Tuesday, September 28th, 2010 11:12

    24

    Unsafe code, don’t use straight without input sanitation.

    -2
    • Kevin Stanley

      Thursday, September 30th, 2010 13:00

      28

      I have mentioned that already. This tutorial is to show the use of HTML forms and PHP. The code should not be used without some form of sanitation.

      +3
  • Christoph

    Tuesday, September 28th, 2010 10:11

    23

    I find this article very useful for learning the basic techniques behind a php contact form (as mentioned before with the need to add certain features, like validation, spam-protection. etc.). What bothers me is the article’s title… today, everything is “kick-ass”, “awesome”, and “breath-taking”. Of course you get the people’s attention with such titles, but sorry, this is a really nice basic tutorial, but it’s not “kick-ass” :)…

    +3
    • Kevin Stanley

      Thursday, September 30th, 2010 13:02

      29

      Don’t hold me responsible for the title. It was posted that way, and I just wrote the tutorial :)

      Validation is a must, as well as spam protection. This tutorial was meant to show the use of HTML forms and PHP.

      Thanks for the comment!

      +3
  • wqs

    Monday, September 27th, 2010 21:25

    22

    cool yaar…very good looking form

    +1
  • Stacy

    Monday, September 27th, 2010 18:52

    20

    Hi – thanks for another excellent tutorial! One question – I know its possible to make input fields mandatory but can’t work out how to do so – can anyone help?

    0
  • Sam

    Monday, September 27th, 2010 18:26

    19

    Kevin, when using a wordpress install do you prefer raw code forms or using a plugin like contact form 7?

    0
    • Kevin Stanley

      Monday, September 27th, 2010 19:27

      11

      Contact Form 7 is excellent!

      0
  • Harsh Agrawal

    Monday, September 27th, 2010 22:23

    18

    Awesome tutorial.. Thanks :)

    0
  • Peter

    Monday, September 27th, 2010 15:31

    17

    What not validation? If someone is using this turorial because they can’t code a form they are not going to know how to validate it with php….

    -1
    • Kevin Stanley

      Monday, September 27th, 2010 19:29

      21

      There are so many different ways to validate. This tutorial is for showing how you can make html forms work with PHP to perform a funciton. Validation should always be done I agree, but the choice is up to the user creating the form. Having a good understanding of how the forms and code works will also better help someone trying to learn to code themselves, and how to implement a CAPTCHA if they choose.

      +3
  • Scorpiono

    Monday, September 27th, 2010 21:13

    16

    Shouldn’t we have a verification if fields are empty or not and if $_POST['submit'] isset? I’m not a PHP developer, I’m just asking.

    0
    • Brian Cline

      Wednesday, October 6th, 2010 05:31

      36

      I would recommend you always validate any forms using javascript as a first line of defence, and then revalidate on the server. It is also wise to validate that the fields (especially email fields) don’t contain any nasty surprises.

      Whether you use a captcha or some other form of antispam is up to you, but I would also recommend this.

      0
  • Geoffrey Gordon

    Monday, September 27th, 2010 15:13

    15

    Great tutorial, how do you connect it to mysql database or is it unnecessary? Excuse my ignorance, I use jotforms, I code a lot in html and css, but learning php, so I want to make the transition and code my own forms.

    0
    • Kevin Stanley

      Monday, September 27th, 2010 19:30

      12

      No MySql needed. Just the HTML form and PHP. As from the other comments, you can see that you still need some form of validation. This will be super easy to add into the code before placing it on a live site.

      0
  • pesho

    Monday, September 27th, 2010 14:31

    14

    Nice
    awesome Tutorial
    thanks

    +1
  • Nicolas

    Monday, September 27th, 2010 13:51

    13

    You forgot validation. Isn’t your code vulnerable to “email header injection”?

    -4
  • roxx

    Monday, September 27th, 2010 13:15

    10

    it is better to sanitize user inputs before sending emails, inserting into db etc..

    -3
  • Suzy

    Monday, September 27th, 2010 12:40

    9

    I like the idea, but its very basic :D i missed the validation part, but great tut for html beginners! Keep on the good work!

    0
  • Ramona

    Monday, September 27th, 2010 12:36

    8

    I’ve never been able to actually make a good form. This tutorial is indeed priceless. Will surely bookmark and promote it myself too.

    0
  • giedrius majauskas

    Monday, September 27th, 2010 12:13

    7

    I think this is the best way to get your website in multiple black lists.
    Why? because this form is superb to spammers. It performs no data sanitization,
    And spammers can construct additional headers into message.
    This allows them to spam other people USING your webserver without you even knowing it.

    +7
  • Jimmy

    Monday, September 27th, 2010 15:29

    6

    Hi,
    It is imperative to sanitize all input data.

    It can be used to evil purposes just by changing the email input to a textarea and typing
    [email protected] \nBcc: [email protected], [email protected], [email protected], …, [email protected]

    Nice blue form tho.

    0
    • Brian Cline

      Wednesday, October 6th, 2010 05:36

      37

      Sanitizing the email field and validating the fields are filled was the first thought I had when I finished the email.

      I believe we are doing any designers or other person building a website a strong disservice when we include code that is said to do exactly what the user needs but includes absolutely no security.

      0
  • Jean-Baptiste

    Monday, September 27th, 2010 15:11

    5

    What about “labels” before the “input” instead of “p” ?
    With of course “for” attributes relative to input’s “id” ?

    Whatever, thanks for the tutorial !

    0
  • Wong

    Monday, September 27th, 2010 14:52

    4

    Love it! But can add in a hidden form and check the form for spam too?

    0
  • Irfan Suleman

    Monday, September 27th, 2010 14:33

    3

    good, keep it up

    0
  • Kevin Stanley

    Monday, September 27th, 2010 13:37

    2

    I agree that there is much more before putting this as is on a live site. The purpose of this tutorial is to show how to get html forms to interact with PHP to perform functions and calls. With validating this form, and adding a captcha, it would be ready. I did add at the footnotes that more modification could be done, and wanted to use the tutorial to let people learn how to build the form itself, while learning to work with forms and PHP. Thank you for the feedback.

    0
  • Dave Mellett

    Monday, September 27th, 2010 12:58

    1

    Nice, clear explanation of how a php contact form works. Hardly kick-ass though, this would have to be seriously upgraded for production.

    0

Comments are closed.

54.166.8.138 - unknown - unknown - US