A contact form is essential to any website, business or personal. It allows the visitor to communicate with the webmaster and offer feedback and suggestions.
Without further ado, let's take a look how you can create your own contact form with PHP: it's easier than you think!
Let's name this file "contact.php" and start coding:
| <html><head> <title>My PHP Mailer</title></head><body> |
The html/head/body tags are a necessary part of every webpage, and now that we have that out of the way we can start creating the form.
| <form name="mailer" method="post" action="mail.php"> |
|
This line tells us that a form is following, and to submit the form to a file called "mail.php". The mail.php file will be constructed on the next page, so don't worry about it just yet. Also note the 'method="post"' line, this tells us that the variables (data submitted through the form) will be sent using POST instead of GET (if you don't know the differences between POST and GET don't worry, that's another subject altogether). |
<p>Your name: <input name="name" type="text" id="name"> </p> <p>Your comments:<br> <textarea name="comments" cols="20" rows="4" wrap="VIRTUAL" id="comments"></textarea> </p> |
The above creates the actual text fields where the data can be entered. One of the more important attributes is "name" - this will tell PHP what that field is named as, so that the data can be actually used in the next page!
<p> <input name="submit" type="submit" id="submit" value="Submit"> </p> </form> </body> </html> |
Now we have created the "Submit" button to take us to the mailing page and "closed" the HTML page. You can customize the text on the button by changing the value attribute if you wish.
But where's the PHP code? I came here to learn PHP!
Don't worry, now that we've laid the foundation we can get to the fun of the actual mailing! But first, let's recap what we've coded in this section:
contact.php
<html> <head> <title>My PHP Mailer</title> </head>
<body> <form name="mailer" method="post" action="mail.php"> <p>Your name: <input name="name" type="text" id="name"> </p> <p>Your comments:<br> <textarea name="comments" cols="20" rows="4" wrap="VIRTUAL" id="comments"></textarea> </p> <p> <input name="submit" type="submit" id="submit" value="Submit"> </p> </form> </body> </html> |
The end product will look something like this:

Ready to go on? Head to page two to complete the script!