Web Hosting and Domain Registration - Dayana Host    Web Hosting with 100% Satisfaction Guarantee by Dayana Host
Cheap Web Hosting and Domain Registration Service.  Web Hosting with 100% Satisfaction Guarantee by Dayana Host
 
 

HOW TO: create a contact us form

Web designing, HTML, Programming in PHP or Perl, Database Scripts,...

Postby lisa » Fri Mar 26, 2004 1:26 pm

1. Create a form with as many as fields you want, name it "contact.php"
2. Create a "thank you" page, name it "thanks.php"
3. In "action" line of contact.php post it to your "thank you" page, it will be something like this:

CODE
<form action='thanks.php' method='POST' >



4. Add this code at the top of your "thank you" page:

CODE

<?php

$ToAddress = "lisa@dayanahost.com";
// To send it to more than one reception use
// coma (,) between addresses.

$message  = "Message from web form\n";
$message .= "-------------------------------------------\n";
foreach ($_POST as $key => $value){$message .= "$key: $value\n";}
$message .= "-------------------------------------------\n";

mail("$ToAddress",
        "Message from web",
         "\r\n".$message,
         "From: $ToAddress\r\nCc:\r\nBcc:\r\n\r\n");

?>





5. Make change to first line of the code with your email address. For example:

CODE
$ToAddress = "PUT_YOUR_EMAIL_ADDRESS_HERE";


to

CODE
$ToAddress = "lisa@dayanahost.com";



6. Now upload both pages to public_html folder of your account.

7. That's it and you are ready to go.

Note: You may change the name of these pages, Just make sure that action line of the contact form addressed to "thank you" page.


Lisa
lisa
Administrators
 
Posts: 82
Joined: Tue Sep 24, 2002 2:17 pm

Postby lisa » Fri Mar 26, 2004 1:30 pm

Here is a sample of both two pages:


contact.php
CODE
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Contact us</title>
</head>

<body>

<h1><b>Contact us</b></h1>
<hr>
<form method="POST" action="thanks.php">
<p>Name: <input type="text" name="name" size="20"></p>
<p>Email: <input type="text" name="email" size="20"></p>
<p>Eye color: <input type="text" name="eye_color" size="20"></p>
<p>Subject: <input type="text" name="subject" size="20"></p>
<p>Message:</p>
<p><textarea rows="10" name="message" cols="42"></textarea></p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>
<p>&nbsp;</p>

</body>

</html>




thanks.php
CODE

<?php

$ToAddress = "lisa@dayanahost.com";

$message  = "Message from web form\n";
$message .= "-------------------------------------------\n";
foreach ($_POST as $key => $value){$message .= "$key: $value\n";}
$message .= "-------------------------------------------\n";

mail("$ToAddress",
       "Message from web",
        "\r\n".$message,
        "From: $ToAddress\r\nCc:\r\nBcc:\r\n\r\n");


?>




<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Thank you</title>
</head>

<body>
<h1><b>Contact us</b></h1>
<hr>

<p><b>We've received your message, We will contact you as soon as possible.</b></p>

</body>

</html>


Lisa
lisa
Administrators
 
Posts: 82
Joined: Tue Sep 24, 2002 2:17 pm

Postby Birdman » Tue Mar 30, 2004 8:10 am

Thanks for the formmail tutorial, Lisa! I'm positive that many will utilize it and learn from it.

One thing I noticed:

You put the $ToAddress in the from parameter. I usually put the email of the user in there so I can simply reply to the email.

mail("<$ToAddress>","Message from web", $message,"From: ".$_POST["email"]);

Thanks again for sharing this info with the users.

Birdman

PS. Hmmm, I guess I should try to add something useful, being the moderator and all.
Birdman
Administrators
 
Posts: 71
Joined: Fri Oct 18, 2002 8:09 am
Location: Maryland

Postby jkoegel » Sun May 23, 2004 6:24 pm

Lisa, thanks for this code. I adapted it line-for-line for a Web site I am developing for my church, and it works fine in IE, but AOL users get a "502 Connection Hangup" error message, although their messages do go through. Do you know how to prevent this from happening?

Also, under Netscape and Mozilla, the line "-----START CODE SECTION //-----END OF THE CODE-->" appears at the top of the "thanks" screen, although the messages do go through.

I am new to Web site development and appreciate your bearing with these "newbie" questions.

Thanks very much.
jkoegel
Newbie
 
Posts: 1
Joined: Sun May 23, 2004 6:18 pm

Postby sunnygrl » Wed Aug 18, 2004 7:07 am

Thanks! It worked and it was quick to set up!
sunnygrl
Advanced Member
 
Posts: 10
Joined: Thu Jan 22, 2004 8:54 pm

Postby admin » Wed Oct 20, 2004 3:54 pm

Please be advise that some security fixes applied to this script to prevent unauthorized usage.

F.Malekpour
Dayana Networks
admin
Administrator
 
Posts: 137
Joined: Sat Jan 04, 2003 12:50 am

Postby dayana » Wed Oct 20, 2004 3:57 pm

QUOTE (Birdman @ Mar 30 2004, 11:10 AM) Thanks for the formmail tutorial, Lisa! I'm positive that many will utilize it and learn from it.

One thing I noticed:

You put the $ToAddress in the from parameter. I usually put the email of the user in there so I can simply reply to the email.

mail("<$ToAddress>","Message from web", $message,"From: ".$_POST["email"]);

Thanks again for sharing this info with the users.

Birdman

PS. Hmmm, I guess I should try to add something useful, being the moderator and all.
That's a good idea, But if sender uses an invalid or malformed email address you can't receive the email.

Dayana
User avatar
dayana
Administrators
 
Posts: 193
Joined: Tue Sep 24, 2002 1:54 pm
Location: Toronto

Postby Birdman » Wed Oct 20, 2004 6:02 pm

QUOTE That's a good idea, But if sender uses an invalid or malformed email address you can't receive the email.

Sure, you still recieve the mail. I know this to be true because I have a few forms set up that way and if user leaves email field blank, it appears to be from: "Apache". I don't see any benefit of sending an email to yourself, from yourself.

It's always good to validate user inputted data too(especially if adding data to a database). So we would catch any malformed emails with this:

CODE if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {
error("Please enter a valid email address!");
}


Error checking is very important but it complicates things as well. Here is a slightly different version of Lisa's much appreciated script. This one will automatically redirect back to the form, with all user data in place if any error are detected.

contact.php
CODE
<?php
if (isset($_GET['error'])){
$name = $_GET['name'];
$email = $_GET['email'];
$eye_color = $_GET['eye_color'];
$subject = $_GET['subject'];
$message = $_GET['message'];
} else {
$name = '';
$email = '';
$eye_color = '';
$subject = '';
$message = '';
}
?>
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Contact us</title>
</head>

<body>

<h1><b>Contact us</b></h1>
<?php if (isset($_GET['error'])) print '<b>Error: ' . $_GET['error'];?>
<hr>
<form method="POST" action="thanks.php">
<p>Name: <input type="text" name="name" size="20" value="<?=$name?>"></p>
<p>Email: <input type="text" name="email" size="20" value="<?=$email?>"></p>
<p>Eye color: <input type="text" name="eye_color" size="20 value="<?=$eye_color?>""></p>
<p>Subject: <input type="text" name="subject" size="20 value="<?=$subject?>""></p>
<p>Message:</p>
<p><textarea rows="10" name="message" cols="42"><?=$message?></textarea></p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>
<p>&nbsp;</p>

</body>

</html>

Now, on some servers(like dayana's), you could actually leave the top block of code out and simply TRY to print the values into the form. If they don't exist, they simply won't be there. But, on other servers, you may get errors for undefined variables. I always do it this way because I'm used to working on another server with strict errors.

Anyhow, what we did was:

A) Check to see if we are in error mode by checking for the existance of the $_GET['error'] variable. $_GET is a global variable which comes from the querystring of a URL. On Dayana's servers, you can access these variables just by there literal name($error), rather than the $_GET[] style. This is because of the PHP register_globals setting. IT IS NOT WISE TO USE THE SHORT METHOD BECAUSE OF CODE INJECTIONS, ETC.

If we are in error mode, we create the variables to repopulate the form. If not in error mode, we just create the variables as empty strings. This is to stop errors on servers with strict error reporting.

C) Then, we just print the variables in the value attributes of the <input> elements.

thank_you.php
CODE <?php

function error($error, $str=''){
foreach ($_POST as $key => $val) $str .= '&' . $key. '=' . $val;
header('Location: contact.php?error=' . $error . $str);
exit;
}

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST["email"])) {
error("Please enter a valid email address!");
}

if (trim($_POST['message']) == '') error('Please enter a message!');
if (trim($_POST['eye_color']) == '') error('Please enter an eye color!');
// and so on...

$ToAddress = "lisa@dayanahost.com";

$message  = "Message from web form\n";
$message .= "-------------------------------------------\n";
foreach ($_POST as $key => $value){$message .= "$key: $value\n";}
$message .= "-------------------------------------------\n";

mail($ToAddress,
      $subject,
       $message,
       'From: ' . $_POST['email'] . '\r\n');
?>




<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Thank you</title>
</head>

<body>
<h1><b>Contact us</b></h1>
<hr>

<p><b>We've received your message, We will contact you as soon as possible.</b></p>

</body>

</html>

Note I also added the user's subject into the email subject line as well.

Have fun!
Birdman
Administrators
 
Posts: 71
Joined: Fri Oct 18, 2002 8:09 am
Location: Maryland

Postby tlynn721 » Thu Nov 18, 2004 3:30 pm

I have been searching high and low for a simple formmail script. This one was so straighforward I thought there was no way I could mess it up. Guess what? When I try to use the "submit" button this is what I get:

The page cannot be displayed
The page you are looking for cannot be displayed because the address is incorrect.

--------------------------------------------------------------------------------

Please try the following:

If you typed the page address in the Address bar, check that it is entered correctly.

Open the tlynn721.tripod.com home page and then look for links to the information you want.
Click Search to look for information on the Internet.



HTTP 405 - Resource not allowed
Internet Explorer

Any idea what the problem could be? I copied everything and didn't change any code.

Thanks!
tlynn721
Newbie
 
Posts: 1
Joined: Thu Nov 18, 2004 3:23 pm

Postby cfrbs » Mon Mar 28, 2005 9:12 pm

Worked great, and very easy! Accept when I tried to add more than one recipient address to the $ToAddress, it didn't work any more. I separated them with a comma, within the quotes? Any ideas?

Thanks!
cfrbs
Newbie
 
Posts: 1
Joined: Mon Mar 28, 2005 9:10 pm

Next

Return to Designing and Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron
 

Home | Process Guide | Affiliate Program | Network Maps | Testimonials | Uptime Monitor | Contact | Table of Contents | phpOnline
Domain Registration | Personal Web Hosting Plans | Business Web Hosting Plans | Reseller Web Hosting Plans | Sample Clients
Customer Service | Cheap Web Hosting | Support Center | Control Panel Demo | Useful Resources and links
CPanel and WHM Tutorials | Web Site Design Templates | Upgrade, Renew and Maintenance | Free Counter | NS Lookup | Whois
IP Location | Acceptable Use Policy, Anti Spam Policy, Terms of Services | Privacy Policy