Friday, October 16, 2009

PHP webform emails XML plain text

The PHP code that generates an email is so short due to using foreach($_POST as $key => $value). I was able to use the key names because, in the HTML form, I named the input fields with full, readable, descriptive names ( i.e. first_name rather than fname ).

The email is encoded as plain text. I originally set it up to send an ugly output from the form. Then the users requested XML. It was amazing how simple that was to add. I'd recommend XML as the structured output, oddly, for the readability alone.

SIMPLEST XML EXAMPLE
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
</note>
<?php
$recipient = "this.is.lance.miller@gmail.com";
$message = "";
$ip = $_SERVER['REMOTE_ADDR'];
$email = "this.is.lance.miller@gmail.com";
if ($_POST['email']) { $email = $_POST['email'] ; }
if ($_POST['submit']) {
$message = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<!-- html/php/xml code by this.is.lance.miller@gmail.com -->\n";
$message .= "<volunteer>\n";
foreach($_POST as $key => $value) {
 if ($key != "submit") { $message .= "\t<$key>\n\t$value\n\t</$key>\n";}
                                  }
$message .="\t<remote_address>\n\t${ip}\n\t</remote_address>\n";
$message .= "</volunteer>\n";
mail( $recipient , "Volunteer Form Submission $email $ip", $message, "From: $email" );
echo "Thank you. Your information has been sent.";
} else {

?>
<h1>Volunteer Application Form</h1>
<div style="padding:5px;background:#CCCCCC">
<form name="volunteer_form" method="POST" action="<?php echo $PHP_SELF;?>">
CONTACT INFORMATION:
<br />
First Name:<input type="text" name="first_name" size="25" />
Last Name:<input type="text" name="last_name" size="25" />
<br />
Address:<input type="text" name="address" size="80" />
<br />
Email:<input type="text" name="email" size="80" />
<br />
Home Phone:<input type="text" name="home_phone" size="16" />
Other Phone:<input type="text" name="other_phone" size="16" />
<br />
Emergency Contact:<input type="text" name="emergency_contact" size="25" />
Phone:<input type="text" name="emergency_contact_phone" size="16" />
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
AVAILABILITY:
<br />
<table><tr>
<td valign="top" align="left">
Weekdays <input type="radio" name="weekdays" value="Yes"> Yes <input type="radio" name="weekdays" value="No"> No 
<br />
Weekends <input type="radio" name="weekends" value="Yes"> Yes <input type="radio" name="weekends" value="No"> No 
<br />
Evenings <input type="radio" name="evenings" value="Yes"> Yes <input type="radio" name="evenings" value="No"> No 
<br />
</td><td valign="top" align="right">
<textarea cols="60" rows="4" name="details">
Details here if needed: 
</textarea>
</td></tr></table>
<br />
What events are you interested in volunteering for at the Abbotsford Entertainment and Sports Center?
<br />
<input type="radio" name="concerts" value="Yes"> Concerts
<input type="radio" name="hockey" value="Yes"> Hockey
<input type="radio" name="theatre" value="Yes"> Theatre
<br />
<input type="radio" name="trade_shows" value="Yes"> Trade Shows
<input type="radio" name="private_events" value="Yes"> Private Events
<br />
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
SKILLS AND EXPERIENCE:
<br />
Do you have a valid Drivers License?
<input type="radio" name="driverslicense" value="Yes"> Yes <input type="radio" name="driverslicense" value="No"> No 
<br />
Do you have a first aid certificate?
<input type="radio" name="first_aidc_ertificate" value="Yes"> Yes <input type="radio" name="first_aid_certificate" value="No"> No 
<br />
Do you have a food safe certificate?
<input type="radio" name="food_safe_certificate" value="Yes"> Yes <input type="radio" name="food_safe_certificate" value="No"> No 
<br />
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
SPECIAL SKILLS:
<br />
<input type="radio" name="av_support" value="Yes"> A/V Support
<input type="radio" name="artistic_skill" value="Yes"> Artistic Skill
<input type="radio" name="child_care" value="Yes"> Child Care
<br />
<input type="radio" name="coach_referee_official" value="Yes"> Coach/Referee/Official
<input type="radio" name="customer_service" value="Yes"> Customer Service
<input type="radio" name="first_aid_certificate" value="Yes"> First Aid Certificate
<br />
<input type="radio" name="public_speaking" value="Yes"> Public Speaking
<input type="radio" name="office_skills" value="Yes"> Office Skills
<input type="radio" name="photography" value="Yes"> Photography
<br />
<input type="radio" name="musical_skills" value="Yes"> Musical Skills
<input type="radio" name="sport_skills" value="Yes"> Sport Skills
<input type="radio" name="teaching_education" value="Yes"> Teaching/Education
<br />
<input type="radio" name="writing_editing" value="Yes"> Writing/Editing
<input type="radio" name="other_skill" value="Yes"> Other <input type="text" id="other_skill_detail" size="25" />
<br />
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
<input type="submit" name="submit" value=" SEND " />
</form>
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
<span style="font-style:monospace;font-size:75%">this.web.form by this.is.lance.miller@gmail.com</span>
</div>
<?php
}
?>

No comments: