
- Drop me a line! Contact me here.
Building A Contact Form
Building A Contact Form
Contact forms are a staple of a modern website. It is vital to give your users a method of sending feedback, and a form that takes input and emails it to the site owner is one such way. Building a contact form is quite simple.
As this is a very simple contact form we'll not worry about returning the inputted information if the user leaves out any vital form fields. We should also note that the form is using the POST method. There are two available submission methods in HTML, GET and POST. The GET method encodes the form input into the query string and submits it as a web page request using the location. This is great for relatively short forms, but it is limited to about 1000 characters, so its not particularly suited to contact forms. Therefore we need to use POST as it doesn't have any such limits.
When the user fills in the form and presses the send button the data is sent to our php script called form.php. This script needs to check the data when the form is submitted, and send it in an email if it is alright, else inform the user something went awry.
We can inform the user that something went wrong by echoing $error in a javascript alert() box.
If the user did fill in all the required fields we need to build up an email and send it to the site owner. Email in PHP is very simple. The mail() command does the entire job for us. We just need to tell mail() what to send.
The mail() command has been preceded with an @ symbol. What this does is suppress any error message doing to the user if the mail() function should fail. Instead we have wrapped the function in an if..else block to catch any errors and fail gracefully if something does occur.
There is much more than could, and should, be built into a contact form. The data should be validated to check for things like email address formatting and message length. Sending the user an email copy of the message is also a good idea.
<form action="form.php" method="POST">
Name: <input type="text" name="name" value="">
Email: <input type="text" name="email" value="">
Subject: <input type="text" name="subject" value="">
Message: <textarea name="message"></textarea>
<input type="submit" name="submit" value="send">
</form>As this is a very simple contact form we'll not worry about returning the inputted information if the user leaves out any vital form fields. We should also note that the form is using the POST method. There are two available submission methods in HTML, GET and POST. The GET method encodes the form input into the query string and submits it as a web page request using the location. This is great for relatively short forms, but it is limited to about 1000 characters, so its not particularly suited to contact forms. Therefore we need to use POST as it doesn't have any such limits.
When the user fills in the form and presses the send button the data is sent to our php script called form.php. This script needs to check the data when the form is submitted, and send it in an email if it is alright, else inform the user something went awry.
<?php
$error = ($_POST['name'] == "") ? "Name\\\n" : $error;
$error = ($_POST['email'] == "") ? "Email\\\n" : $error;
$error = ($_POST['name'] == "") ? "Subject\\\n" : $error;
$error = ($_POST['message'] == "") ? "Message" : $error;
if ($error == "") {
//send email
} else {
$error = "Missing fields\\\n".$error;
}
?>
We can inform the user that something went wrong by echoing $error in a javascript alert() box.
<?php
if ($error != "") {
echo "<script>";
echo "alert('".$error."');";
echo "</script>";
}
?>
If the user did fill in all the required fields we need to build up an email and send it to the site owner. Email in PHP is very simple. The mail() command does the entire job for us. We just need to tell mail() what to send.
<?php
$to = "chris@ooer.com";
$subject = $_POST['subject'];
$body = "Name: ".$_POST['name']."\n";
$body .= "From: ".$_POST['email']."\n";
$body .= "Message: ".$_POST['message']."\n";
if (@mail($to,$subject,$body)) {
//Thank you message
} else {
//Error message
}
?>
The mail() command has been preceded with an @ symbol. What this does is suppress any error message doing to the user if the mail() function should fail. Instead we have wrapped the function in an if..else block to catch any errors and fail gracefully if something does occur.
There is much more than could, and should, be built into a contact form. The data should be validated to check for things like email address formatting and message length. Sending the user an email copy of the message is also a good idea.
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 3
- DB Time: 0.0666 seconds
