Welcome, Guest

Please login or register

TUTORIALS SUBMENU ---->

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING

Related Links

Contact Forms


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.

- Tutorial written by Scrowler

Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments

User:  Hooch (#23671)
Date: Tue Feb 07, 2006. 00:28:47

Post #3 of 3

This looks like a great form 72.
Now how do I implement this? I copied the whole code and made the contact.php file.
Since I can't directly link to it..I changed the name to contact1.php. I then could see the page, and I got it to send an email. But ofcourse the link back didn't work.
So how does this work with my site?
The main page will be an html.
I hope I am clear enough.
TY, Hooch

Reply to this post


User:  scrowler (#19667)
Date: Mon Oct 31, 2005. 10:09:36

Post #2 of 3

why not write this up as an "advanced contact forms" tutorial and submit it?

Reply to this post


User:  72dpi (#18222)
Date: Fri Sep 02, 2005. 08:09:13

Post #1 of 3

Here is the same method that I use,
It also includes a autoresponse.

just change the content as needed.





Untitled Document




// keep people from accessing this page directly
if (eregi('contact.php', $_SERVER['PHP_SELF'])) {
// go to index page
header('Location: ../main.php?page=contact');
die();
}

?>

Contact Form


echo "

* Denotes required fields.

";

// put your email here
$send_to = "youremail@your.com";

// This is what is displayed in the email subject line
$subject = "Message from Your Website";

// This is displayed if all the fields are not filled in
$empty_fields_message = "

Please go back and complete all the fields in the form!

";

// This is displayed when the email has been sent
$thankyou_message = "

Thankyou. Your message has been sent! =)

<< Back to Main

";

// set up info
$nowDay=date("m.d.Y");
$nowClock=date("H:i:s");
$http_referrer = getenv( "HTTP_REFERER" );

// You do not need to edit below this line

$name = stripslashes($_POST['txtName']);
$email = stripslashes($_POST['txtEmail']);
$phone = stripslashes($_POST['txtPhone']);
$message = stripslashes($_POST['txtMessage']);

if (!isset($_POST['txtName'])) {

?>



Name: *


Email: *



Phone:



Contact?



Yes


No

How did you find us?


Advertisement

Word of Mouth

Search Engine

Web Link

Other

Message: *



 



}
elseif (empty($name) || empty($email) || empty($message)) {

echo @$empty_fields_message;
}
else {
// Stop the form being used from an external URL
// Get the referring URL
@$referer = $_SERVER['HTTP_REFERER'];
// Get the URL of this page
@$this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
// If the referring URL and the URL of this page don't match then
// display a message and don't send the email.
if ($referer != $this_url) {
echo "You do not have permission to use this script from another URL.";
exit;
}
@$messageproper =

// we now generate a nice form for emailing
"Message recieved: $nowDay at $nowClock:\n\n" .
"Email Sent From: $http_referrer\n\n" .
"Name: $name\n\n".
"Email: $email\n\n".
"Phone Number: $phone\n\n".
"Contact?: $contact\n\n".
"Found site By: $found\n\n".

"------------------------ COMMENTS ------------------------\n\n\n" .

$message .

"\n\n\n----------------------------------------------------------\n" ;
// The URLs matched so send the email
mail($send_to, $subject, $messageproper, "From: $name <$email>");

# Autoresponder #############################################################
// note , the . (dot) joins two strings. to end a string, use semi-colon ;
$replysubject =
"Query from Your Website" ; // send our subject header
$replymessage =
"Dear " .
$name .
"\n\nThank you for your email. \nIf you had so requested, we will get back to you as soon as possible. " .
"\n\nRegards," .
"\n Your Name Here" .

mail( "$email", "$replysubject", "$replymessage\n\n", "From:$send_to" );
// Display the thankyou message
echo $thankyou_message;

}

?>





Hope it helps

Reply to this post


--- View Entire Thread ---
Featured Content

Retro Style Text
Retro Style Text
- Adobe Photoshop -
Abstract Wireframes
Abstract Wireframes
- Adobe Photoshop -
Simple Sound Contr...
Simple Sound Contr...
- Macromedia Flash -
Pixel Stretch
Pixel Stretch
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Special Options
Download Source File
Printer Friendly Version
Forum Threads

Competition Discussion - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:48pm
Activity: 0 replies, 53 views
 Competition - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:46pm
Activity: 0 replies, 54 views
 PM Spamming
Author: Tamlin
Posted: Feb 06th, 1:24pm
Activity: 7 replies, 115 views
Vector Clipart Bank
Author: Crapoun
Posted: Feb 06th, 11:29am
Activity: 2 replies, 93 views
How did ...
Author: MoodsR4Cattle
Posted: Feb 05th, 6:09pm
Activity: 6 replies, 26 views
Tips and trick for Texturing/Materials
Author: noorjan
Posted: Feb 05th, 4:59am
Activity: 2 replies, 108 views
 A Billion Styles - Please Help Me!!
Author: Angelz
Posted: Feb 03rd, 6:36pm
Activity: 2 replies, 133 views
101 Things you didnt know in 3DS Max ...in fact...
Author: noorjan
Posted: Jan 31st, 6:04pm
Activity: 0 replies, 160 views
Pee Wee get's an IPad
Author: MoodsR4Cattle
Posted: Jan 30th, 4:25pm
Activity: 2 replies, 163 views
Spam :: Online hotel reservations for Hotels in...
Author: kieulinh
Posted: Jan 28th, 6:39am
Activity: 0 replies, 204 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 3 replies, 17 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 27 replies, 727 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    415