Downloaded from www.biorust.com on Sat Nov 21, 2009 21:31:34

 
Contact Forms
Tutorial Author - Scrowler (http://forums.biorust.com/member.php?userid=66)

So, you've just finished your latest layout and want to put in a contact form. This is very easy with a little PHP coding - So easy, in fact, that this tutorial can be understood by just about anyone, and the code itself is also mystifyingly short considering its ability to benefit your website.

For this tutorial, we will only use one file, and I’ve written it so that you can name the file anything you like and it will still work - this is the glory of using $_SERVER[‘PHP_SELF’] as the form action!   :D

We start our script with the following code:

if(isset($_POST['sendMail']))
{

$to = "your@email.com";
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);

if(((empty($name))||(empty($email))||(empty($message))))
{

echo 'Sorry, you forgot to fill out some required fields. ';
echo '<a href="javascript:history.back(1)">Try again</a>';

} else {

if(!strstr($email , "@"))
{

echo 'Please enter a valid email address. ';
echo '<a href="javascript:history.back(1)">Try again</a>';

} else {

$send = mail($to , "Contact message from YourWebsite" , "This email was sent from your website.\n\n".$name." send this message from ".$email.":\n\n".$message."\n\nClick reply and it will send to this email." , "From: Contact Form Reply-to: ".$email);

if($send)
{

echo 'Mail sent successfully.';

} else {

echo 'There was an error sending the mail!';

}
}
}

Woah!  This may look like a very full page of text, but the actual code itself is very simple, and includes a few protection procedures to make sure that your user is actually submitting mail. Of course, none of the errors will show to the user when he uses the form - they are just there in case somebody thinks they can be smart and spam the script.

Right at the start of this script we define our post globals as regular variables, so that we don’t have to put in the $_POST[‘blahblah’] bit, and we can just write $blahblah. The function trim() is also used, which simply takes spaces off the start and finish of a string, so if the user tried to put thousands of spaces into the end of his email, you won’t receive it, as trim() would have already cut it off.

The first crucial nested IF statement checks that none of the fields are empty, and if one of them is, it sends the user an error and a link to try again. In the ELSE option, we have more processing - firstly an IF statement that uses strstr() to check the email address for validity, and if it is invalid, the user gets prompted to try again, just as before. If the email is valid, the processing continues, sends the email, and then uses our final IF statement to check if it was sent or not, and outputs the appropriate message.

Well, that's it for the complex bit!  Here's the next half of the script that deals with formatting:

} else {

echo '

<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr valign="top">
<td>Your name </td>
<td width="50%"><form name="contact" method="post" action="'.$_SERVER["PHP_SELF"].'">
<input name="name" type="text" size="32">
</form></td>
</tr>
<tr valign="top">
<td>Your email address </td>
<td><input name="email" type="text" size="32"></td>
</tr>
<tr valign="top">
<td>Your message</td>
<td><textarea name="message" cols="32" rows="10"></textarea></td>
</tr>
<tr valign="top">
<td>Submit</td>
<td>
<input type="submit" name="sendMail" value="Send!">
</form></td>
</tr>
</table>
</body>
</html>

';

}

So, what does this do? Well, quite simply, if the user hasn't already processed the form, PHP proudly presents the user with the form to fill out! The form itself is self explanatory, as it is regular HTML which can be modified as much as you like - provided, of course, that the input field names remain the same, the form method remains POST, and the form action remains .$_SERVER[‘PHP_SELF’]. You can edit the HTML yourself as much as you like really, but leave all the HTML within PHP control or your script will end up looking very silly.

$_SERVER[‘PHP_SELF’] returns the name of the script as shown in the header. So, logically, you can call your contact file anything, and it will process the form onto itself, thereby giving the most versatility!

To change where the email goes to, change the variable $to at the top of the script to your e-mail address.

Other user serviceable parts are as follows:
mail() function
All echo() functions
HTML code (provided it remains inside echo() function


If you need any help with this tutorial, don’t hesitate to contact me via the Creative Forums - my username is scrowler and I am always available there. You can click my name below to visit my profile, where there is contact information and a Private Messaging service. You may have to register.




All Content © BioRUST 2009 All Rights Reserved.