How to create a simple contact form in PHP
2009-08-30 19:02:52
An online contact form is quite a handy feature for your website. It can be used as an additional way for your visitors to contact you, and if you don't feel like displaying your email address for those nasty spammers to get hold of it, you don't have to. This article explains how you would create a simple contact form for your website.
To create an online contact form, you will need two parts.
1) The HTML for the form that is used by your visitors.
2) The PHP server-side code that interprets the form and sends it as an email.
Your online contact form will look something like this:
So let's take a closer look at the key parts of this:
This opens up the form so that the web page knows to treat everything inside as part of the form.
method='post' is used so that the data is sent behind the scenes to the server using the HTTP POST method. The alternative would be to use the HTTP GET method which would make the data visible within the URL. This can be messy, as the user's URL will contain the whole text of the message along with all other submitted information. Also there is a limit to how much data can be sent through GET, whereas POST is not limited.
This is a handy little piece of HTML. It will allow the user to click on the words "Your name: " and it will activate the associated input box.
This is where the name is entered into the form.
This is where the message is entered. Unlike the inputs, this will appear as a box with multiple rows, rather than a single line.
This is the Submit button. It is a special type of input that will submit the HTML form, according to its method. In this case we chose the POST method. The value will display the words "Send message" onto the button.
Great! That's the HTML sorted. Now onto the PHP...
This starts the PHP code
This is only called if the user clicked the Submit button with value "Send message"
Defines the email address that messages will be sent to and the subject that will be given to these messages
Choose the character set. This is the default character set which you would normally use in English language. The \n is the newline character.
This sets the message as from the person and email address specified. Once again \n is used as a newline character.
.= is used instead of equals on its own, because the header is being added to the existing $email_headers variable.
This runs the mail function and also an if statement that checks if the mail() function was successful or not
If the mail function was successful, the above variable is assigned
If the mail function failed, the above variable is assigned.
This closes the PHP code.
The only thing that is left to do now is display the $message_confirmation, which can be done like so:
So your whole page will be:
To create an online contact form, you will need two parts.
1) The HTML for the form that is used by your visitors.
2) The PHP server-side code that interprets the form and sends it as an email.
Your online contact form will look something like this:
<form method='post'>
<table>
<tr>
<td>
<label for='from_name'>Your name: </label>
</td>
<td>
<input name='from_name' type='text' />
</td>
</tr>
<tr>
<td>
<label for='from_mail'>e-mail address: </label>
</td>
<td>
<input name='from_mail' type='text' />
</td>
</tr>
<tr>
<td>
<label for='message'>Message:</label>
</td>
<td>
<textarea name='message'></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input name='submit' id='submit' type='submit' value='Send message' />
</td>
</tr>
</table>
</form>
<table>
<tr>
<td>
<label for='from_name'>Your name: </label>
</td>
<td>
<input name='from_name' type='text' />
</td>
</tr>
<tr>
<td>
<label for='from_mail'>e-mail address: </label>
</td>
<td>
<input name='from_mail' type='text' />
</td>
</tr>
<tr>
<td>
<label for='message'>Message:</label>
</td>
<td>
<textarea name='message'></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input name='submit' id='submit' type='submit' value='Send message' />
</td>
</tr>
</table>
</form>
So let's take a closer look at the key parts of this:
<form method='post'>
This opens up the form so that the web page knows to treat everything inside as part of the form.
method='post' is used so that the data is sent behind the scenes to the server using the HTTP POST method. The alternative would be to use the HTTP GET method which would make the data visible within the URL. This can be messy, as the user's URL will contain the whole text of the message along with all other submitted information. Also there is a limit to how much data can be sent through GET, whereas POST is not limited.
<label for='from_name'>Your name: </label>
This is a handy little piece of HTML. It will allow the user to click on the words "Your name: " and it will activate the associated input box.
<input name='from_name' type='text' />
This is where the name is entered into the form.
<textarea name='message'></textarea>
This is where the message is entered. Unlike the inputs, this will appear as a box with multiple rows, rather than a single line.
<input name='submit' id='submit' type='submit' value='Send message' />
This is the Submit button. It is a special type of input that will submit the HTML form, according to its method. In this case we chose the POST method. The value will display the words "Send message" onto the button.
Great! That's the HTML sorted. Now onto the PHP...
<?
if($_POST["submit"] == "Send message")
{
$to_address = "your@address.here";
$email_subject = "Subject of the emails";
$email_headers = "Content-type: text/html; charset=iso-8859-1\n";
$email_headers .= "From: ".$_POST["from_name"]."<".$_POST["from_mail"].">\n";
if(mail($to_address, $email_subject, $_POST["message"], $email_headers) == true)
{
$message_confirmation = "Your message has been sent<br/>";
}
else
{
$message_confirmation = "Your message could not be sent<br/>";
}
}
?>
if($_POST["submit"] == "Send message")
{
$to_address = "your@address.here";
$email_subject = "Subject of the emails";
$email_headers = "Content-type: text/html; charset=iso-8859-1\n";
$email_headers .= "From: ".$_POST["from_name"]."<".$_POST["from_mail"].">\n";
if(mail($to_address, $email_subject, $_POST["message"], $email_headers) == true)
{
$message_confirmation = "Your message has been sent<br/>";
}
else
{
$message_confirmation = "Your message could not be sent<br/>";
}
}
?>
<?
This starts the PHP code
if($_POST["submit"] == "Send message")
{
{
This is only called if the user clicked the Submit button with value "Send message"
$to_address = "your@address.here";
$email_subject = "Subject of the emails";
$email_subject = "Subject of the emails";
Defines the email address that messages will be sent to and the subject that will be given to these messages
$email_headers = "Content-type: text/html; charset=iso-8859-1\n";
Choose the character set. This is the default character set which you would normally use in English language. The \n is the newline character.
$email_headers .= "From: ".$_POST["from_name"]."<".$_POST["from_mail"].">\n";
This sets the message as from the person and email address specified. Once again \n is used as a newline character.
.= is used instead of equals on its own, because the header is being added to the existing $email_headers variable.
if(mail($to_address, $email_subject, $_POST["message"], $email_headers) == true)
This runs the mail function and also an if statement that checks if the mail() function was successful or not
{
$message_confirmation = "Your message has been sent<br/>";
}
$message_confirmation = "Your message has been sent<br/>";
}
If the mail function was successful, the above variable is assigned
else
{
$message_confirmation = "Your message could not be sent<br/>";
}
{
$message_confirmation = "Your message could not be sent<br/>";
}
If the mail function failed, the above variable is assigned.
?>
This closes the PHP code.
The only thing that is left to do now is display the $message_confirmation, which can be done like so:
<? echo $message_confirmation; ?>
So your whole page will be:
<?
if($_POST["submit"] == "Send message")
{
$to_address = "your@address.here";
$email_subject = "Subject of the emails";
$email_headers = "Content-type: text/html; charset=iso-8859-1
";
$email_headers .= "From: ".$_POST["from_name"]."<".$_POST["from_mail"].">
";
if(mail($to_address, $email_subject, $_POST["message"], $email_headers) == true)
{
$message_confirmation = "Your message has been sent<br/>";
}
else
{
$message_confirmation = "Your message could not be sent<br/>";
}
}
?>
<html>
<body>
<? echo $message_confirmation; ?>
<form method='post'>
<table>
<tr>
<td>
<label for='from_name'>Your name: </label>
</td>
<td>
<input name='from_name' type='text' />
</td>
</tr>
<tr>
<td>
<label for='from_mail'>e-mail address: </label>
</td>
<td>
<input name='from_mail' type='text' />
</td>
</tr>
<tr>
<td>
<label for='message'>Message:</label>
</td>
<td>
<textarea name='message'></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input name='submit' id='submit' type='submit' value='Send message' />
</td>
</tr>
</table>
</form>
</body>
</html>
if($_POST["submit"] == "Send message")
{
$to_address = "your@address.here";
$email_subject = "Subject of the emails";
$email_headers = "Content-type: text/html; charset=iso-8859-1
";
$email_headers .= "From: ".$_POST["from_name"]."<".$_POST["from_mail"].">
";
if(mail($to_address, $email_subject, $_POST["message"], $email_headers) == true)
{
$message_confirmation = "Your message has been sent<br/>";
}
else
{
$message_confirmation = "Your message could not be sent<br/>";
}
}
?>
<html>
<body>
<? echo $message_confirmation; ?>
<form method='post'>
<table>
<tr>
<td>
<label for='from_name'>Your name: </label>
</td>
<td>
<input name='from_name' type='text' />
</td>
</tr>
<tr>
<td>
<label for='from_mail'>e-mail address: </label>
</td>
<td>
<input name='from_mail' type='text' />
</td>
</tr>
<tr>
<td>
<label for='message'>Message:</label>
</td>
<td>
<textarea name='message'></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input name='submit' id='submit' type='submit' value='Send message' />
</td>
</tr>
</table>
</form>
</body>
</html>