Okay then. I’ll show you how to make a very simple contact form and have it send you a mail.
However, before you do this, take a minute to consider some of the alternatives.
For example a quick Google search for “contact form generator” points you to a whole host of services (some free, some not), offering to do what you want.
Anyway, presuming you want to code one by hand, you’ll need a form.
Here’s one I made earlier. Name it contact.php and upload it to your server.
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Contact form example</title>
</head>
<body>
<h1>Send us a message</h1>
<form action="contact.php" method="post">
<label for="message">Your message:</label><br />
<textarea name="message" id="message" rows="10" cols="50"></textarea><br />
<input type="submit" value="Send" />
</form>
</body>
</html>
As it stands, this doesn’t really do much.
When a visitor enters a message, the form just posts this message back to itself and that’s it.
What we would like to do now, is add some PHP code, which can extract the message into an email and send it.
We can do this by adding the following PHP code to the top of our file. Note the use of the strip_tags() function to protect against xxs.
<?php
$message= strip_tags($_POST['message']);
$address = "youremail@yourdomain.com";
$subject = "Message from contact form";
$from = "From: Contact Form <noreply@yourdomain.com>";
mail($address, $subject, $message, $from);
?>
This now poses us with an additional problem, however. When the page loads, the PHP kicks in and sends an empty email to whichever address you specified.
We can get around this by adding the following HTML to the bottom of the form:
<input type="hidden" name="process" value="1" />
and then checking the value of “process”, before executing any PHP.
if ($_POST['process'] == 1) {
...
}
At this point we have a working form, but there are still a couple of issues that need ironing out.
Firstly, it would be sensible to check that the user has entered a message and not just submitted a blank form.
To do this, let’s add an $error variable to the top of our script. Then, we can check if the $message variable is empty. If it is, we can then add our error message to our $error variable.
$error = "";
...
if (empty($message)){
$error.= "You didn't enter a message.";
}
Then, before sending the mail, we can check if the $error variable contains anything. If it doesn’t, then we can proceed as normal, if it does, then we don’t send the mail, rather re-render the form, plus error message.
if (empty($error )) {
send the mail as above
}
...
if (!empty($error )){
echo "<p>Your message could not be sent:</p>";
echo "<p>"."$error"."</p>";
}
Now, this stops a user submitting a blank message, but conversely, it would probably be nice to give the user some feedback if their message is sent correctly.
I normally do this by creating a “success” page, and on a successful form submission, redirecting the user to that page.
You can do this using the header() function in PHP, thus:
header("location: http://www.yoursite.com/contact/success.html");
Finally, although it’s not applicable in our case, as we only have one field, it is nicer for the user if when the form is re-rendered in the case of an error, not all of the fields which they filled out correctly are then wiped.
You can make sure that text fields retain their original values, by using the value attribute thus:
value="<?php echo($message); ?>"
In the case of a text area however, we don’t even need this and can just use
<?php echo($message); ?>
inbetween the <textarea> tags.
Here is the complete script. I hope you find this useful.
<?php
if ($_POST['process'] == 1) {
$message = strip_tags($_POST['message']);
$error = "";
if (empty($message)){
$error.= "You didn't enter a message.";
}
if (empty($error )) {
$address = "youremail@yourdomain.com";
$subject = "Message from contact form";
$from = "From: Contact Form <noreply@yourdomain.com>";
mail($address, $subject, $message, $from);
header("location: http://www.yourdomain.com/contact/success.html");
}
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Contact form example</title>
</head>
<body>
<?php
if (!empty($error )){
echo "<p>Your message could not be sent:</p>";
echo "<p>"."$error"."</p>";
}
?>
<h1>Send us a message</h1>
<form action="contact.php" method="post">
<label for="message">Your message:</label><br />
<textarea name="message" id="message" rows="10" cols="50"><?php echo($message); ?></textarea><br />
<input type="hidden" name="process" value="1" />
<input type="submit" value="Send" />
</form>
</body>
</html>