Wynnefield
07-08-2005, 02:40 AM
I looked all over the internet for a good example of passing HTML Form data to a PHP page to process the data and send an email. I thought I might save someone the trouble I had and post my use of the process here ... the pages look like this:
contact.html:
<form action="contact.php" method="post" name="contact" id="contact">
...
Name: <input name="contact_name" type="text" class="style14" id="contact_name" size="30" maxlength="30">
Company: <input name="contact_company" type="text" class="style14" id="contact_company" size="50" maxlength="50">
Email: <input name="contact_email" type="text" class="style14" id="contact_email" size="50" maxlength="50">
Phone: <input name="contact_phone" type="text" class="style14" id="contact_phone" size="20">
Topic:<select name="contact_topic" size="1" class="style14" id="contact_topic">
<option>Accessories</option>
<option>Advertising</option>
<option>Cigar reviews</option>
<option>Cuban Corner</option>
<option>Herfs & Events</option>
<option>Manufacturers</option>
<option>Retailers</option>
<option>Subscribe</option>
<option>Unsubscribe</option>
</select>
Comments: <textarea name="contact_comments" cols="50" rows="4" class="style14" id="contact_comments"></textarea>
<input type="submit" name="Submit" value="Submit">
...
</form>
contact.php:
<?php
/* receive and email form data from contact.html */
$name = $_POST['contact_name'];
$company = $_POST['contact_company'];
$email = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$topic = $_POST['contact_topic'];
$comments = $_POST['contact_comments'];
$comments = "Name: $name, Company: $company, Phone: $phone, Comments: $comments";
$subject = "tcW.com: $topic";
$fromemail = "From: $email";
$formsent = mail('editor@thecigarwrapper.com', $subject, $comments, $fromemail);
if ($formsent)
{
echo "Thank you, $name. We have received your comments, and will do our best to respond within the next 48 hours.";
} else
{
echo "We are currently experiencing problems with our contact form and apologize for any inconvenience.
Please try again at another time.";
}
?>
Both pages share the same template as far as the page banner, navigation pane and copyright and usage narrative. The PHP code simply replaces the HTML form code in the same place on the page, so the "thank you" message displays and leaves the same navigation options to the site visitor, so he/she can continue surfing.
To see this in action, please feel free to visit thecigarWRAPPER Contact Page (http://www.thecigarwrapper.com/contact.html) ... thank you for reading this example. I hope it helps make the process easier for another web jammer.
Wynne
contact.html:
<form action="contact.php" method="post" name="contact" id="contact">
...
Name: <input name="contact_name" type="text" class="style14" id="contact_name" size="30" maxlength="30">
Company: <input name="contact_company" type="text" class="style14" id="contact_company" size="50" maxlength="50">
Email: <input name="contact_email" type="text" class="style14" id="contact_email" size="50" maxlength="50">
Phone: <input name="contact_phone" type="text" class="style14" id="contact_phone" size="20">
Topic:<select name="contact_topic" size="1" class="style14" id="contact_topic">
<option>Accessories</option>
<option>Advertising</option>
<option>Cigar reviews</option>
<option>Cuban Corner</option>
<option>Herfs & Events</option>
<option>Manufacturers</option>
<option>Retailers</option>
<option>Subscribe</option>
<option>Unsubscribe</option>
</select>
Comments: <textarea name="contact_comments" cols="50" rows="4" class="style14" id="contact_comments"></textarea>
<input type="submit" name="Submit" value="Submit">
...
</form>
contact.php:
<?php
/* receive and email form data from contact.html */
$name = $_POST['contact_name'];
$company = $_POST['contact_company'];
$email = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
$topic = $_POST['contact_topic'];
$comments = $_POST['contact_comments'];
$comments = "Name: $name, Company: $company, Phone: $phone, Comments: $comments";
$subject = "tcW.com: $topic";
$fromemail = "From: $email";
$formsent = mail('editor@thecigarwrapper.com', $subject, $comments, $fromemail);
if ($formsent)
{
echo "Thank you, $name. We have received your comments, and will do our best to respond within the next 48 hours.";
} else
{
echo "We are currently experiencing problems with our contact form and apologize for any inconvenience.
Please try again at another time.";
}
?>
Both pages share the same template as far as the page banner, navigation pane and copyright and usage narrative. The PHP code simply replaces the HTML form code in the same place on the page, so the "thank you" message displays and leaves the same navigation options to the site visitor, so he/she can continue surfing.
To see this in action, please feel free to visit thecigarWRAPPER Contact Page (http://www.thecigarwrapper.com/contact.html) ... thank you for reading this example. I hope it helps make the process easier for another web jammer.
Wynne