|
|
|
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
| 
|
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 |
|
|
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'])) {
?>
}
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 ---
|

|
|
 |
Sketching tut Author: Ratatoskr Posted: Jan 06th, 11:12am Activity: 4 replies, 42 views
|  | Notification of Obsolete Data Purge - IMPORTANT! Author: Man1c M0g Posted: Jan 05th, 11:44pm Activity: 0 replies, 39 views
|  | Multiples Prevention Author: Man1c M0g Posted: Jan 05th, 9:40pm Activity: 0 replies, 9 views
|  | Yeshua crucified on the Cross - pencil art painted Author: horseman Posted: Jan 04th, 2:38am Activity: 6 replies, 104 views
|  | Make a Shape from a JPG Author: dgonzales Posted: Jan 03rd, 10:38pm Activity: 2 replies, 112 views
|  | Premium Subscribers System Author: Man1c M0g Posted: Jan 03rd, 6:00pm Activity: 12 replies, 108 views
|  | Imitating Pencil Shading Author: Chamenas Posted: Jan 03rd, 3:32pm Activity: 0 replies, 71 views
|  | help : How to draw A screw in Rhino 3d Author: Raphy Posted: Jan 01st, 9:39pm Activity: 1 replies, 99 views
|  | bored Author: c-fish819 Posted: Dec 31st, 9:35pm Activity: 1 replies, 99 views
|  | Need A/C in your auto? Author: synthetic Posted: Dec 31st, 3:17pm Activity: 2 replies, 97 views
|  | Brilliant Photochop Collection Author: synthetic Posted: Dec 31st, 2:47am Activity: 2 replies, 153 views
|  | My websites Author: Josh Davidson Posted: Dec 29th, 11:30pm Activity: 3 replies, 117 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 210 |
| Total Downloads: | 413 |
| Linkbase Links: | 243 |
 |
|
 |
 |
|