The HTML Form
You have probably already read about the mail function, and now want to build a more complex form to use on your site. Let's start with the HTML for the web form.
<html><body><font face=Arial size=2>
<form method="post" action="contact.php">
<table bgcolor=#ffffcc align=center>
<tr><td colspan=2><strong>Contact us using this form:</strong></td></tr>
<tr><td>Department:</td><td><select name="sendto"> <option value="info@mycompany.com">General</option> <option value="support@mycompany.com">Support</option> <option value="sales@mycompany.com">Sales</option> </select></td></tr>
<tr><td><font color=red>*</font> Name:</td><td><input size=25 name="Name"></td></tr>
<tr><td><font color=red>*</font> Email:</td><td><input size=25 name="Email"></td></tr>
<tr><td>Company:</td><td><input size=25 name="Company"></td></tr>
<tr><td>Phone:</td><td><input size=25 name="Phone"></td></tr>
<tr><td>Subscribe to<br> mailing list:</td><td><input type="radio" name="list" value="No"> No Thanks<br> <input type="radio" name="list" value="Yes" checked> Yes, keep me informed<br></td></tr>
<tr><td colspan=2>Message:</td></tr>
<tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr>
<tr><td colspan=2 align=center><input type=submit name="send" value="Submit"></td></tr>
<tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr>
</table>
</form>
</body>
</html>
The Form Explained
The code on the last page collects seven pieces of data which we will explain here. It then submits the answers to a page called contact.php which we will create next.
First we use the option feature to create a drop down menu, allowing the user to direct their mail to different departments. Each of these values is set to a different email address. When submitted the form data will be directed to the appropriate email.
Next we collect information about the customer including their Name, Email, Company and Phone Number.
The user is then prompted to join our mailing list, and a radio button is set to default to "Yes".
Finally the user is given a chance to submit the body of their message in large text area.
This form could be copy-pasted into an existing page, or used as is. Don't feel bound to using these categories, you can collect as much or little information as you want using any HTML form input types.
Processing With PHP
Now we must create the PHP for the page contact.php, to collect the data and send it to the appropriate email.
<?php
$to = $_REQUEST['sendto'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();
$fields{"Name"} = "Name";
$fields{"Company"} = "Company";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"list"} = "Mailing List";
$fields{"Message"} = "Message";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: noreply@YourCompany.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://www.YourDomain.com/thankyou.html" );}
else
{print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; }
}
}
?>
On the next page we will explain what this script does.
The PHP Explained
The first three variables we assign in our program ($to, $from, and $name) are all pulled directly from the form data using the $_REQUEST tag. We then define the from header, and a subject for our email (this can be anything you wish.)
Next we create an array; the data on the left is the form fields and the data on the right is the 'Human Friendly" label for each type of content. For example list is our mailing list field from our form, and Mailing List is its label. If you added other fields, or removed some from our example form, you must edit this array appropriately.
Next we define the message body. We use $body to first say what we are sending in the mail (We have received the following information) and then use a FOREACH loop to retrieve the data and its appropriate label from our array. We utilize sprintf() and its associated tags to make sure our data is in string format.
Next we define $headers2, $subject2 and $autoreply. These are the values we will use in our mail to the customer to let them know their form data has been sent to us. You can adjust these all as you see fit.
Finally we check that our required fields (Name and Email) were not left blank before sending the email to our site staff and to the customer.
If this is successful, the user is directed (via the header location tag) to a thank you page. If you prefer you can simply have it print out a success message, similar to our error messages.
No comments:
Post a Comment