Week 6
August 18, 2008 – 9:46 amSession 1: Today we will start developing the PHP required to process the information collected in our forms. PHP (PHP Hypertext Pre-processor) is a server-side scripting language that has been designed specifically for web application development. Using PHP will we collect the data from the form, validated any required fields, compile the data into a message format, and email the results to ourselves.
In order to validate the information efficiently it is important to make sure that the form itself is a PHP document. PHP documents may be a combination of HTML, Javascript, CSS and PHP. In this case it is mostly XHTML, but take note of the PHP instructions dispersed throughout this example:
<form action="<?php echo $_SERVER[PHP_SELF] ?>" method="post" name="form_demo" onSubmit="return validate_form('Form Demo');">
<h3>Personal Information</h3>
<fieldset>
<label class="field-first">First Name*
<input type="text" name="first_name" value="<?php echo $_POST[first_name] ?>" />
</label>
<label class="field-last">Last Name*
<input type="text" name="last_name" value="<?php echo $_POST[last_name] ?>" />
</label>
<label class="field-address">Home Address*
<input type="text" name="address" value="<?php echo $_POST[address] ?>" />
</label>
<label class="field-city">City*
<input type="text" name="city" value="<?php echo $_POST[city] ?>" />
</label>
<label class="field-state">State*
<select name="state">
<option value="MN">MN</option>
<option value="WI">WI</option>
<option value="IO">IO</option>
<option value="IL">IL</option>
<option value="MI">MI</option>
</select>
</label>
<label class="field-postal">Postal Code*
<input type="text" name="postal" value="<?php echo $_POST[postal] ?>" />
</label>
<br style="clear: left;" />
<label class="field-email">Email*
<input type="text" name="email" value="<?php echo $_POST[email] ?>" />
</label>
<label class="field-phone">Phone
<input type="text" name="phone" value="<?php echo $_POST[phone] ?>" />
</label>
</fieldset>
<h3>How often do you eat sushi?</h3>
<fieldset>
<label class="radioitem" for="radiobutton1"><input id="radiobutton1" type="radio" name="sushi" value="Raw fish? Gross!" />Raw fish? Gross!</label>
<label class="radioitem" for="radiobutton2"><input id="radiobutton2" type="radio" name="sushi" value="" />Once in a while</label>
<label class="radioitem" for="radiobutton3"><input id="radiobutton3" type="radio" name="sushi" value="" />Every payday</label>
<label class="radioitem" for="radiobutton4"><input id="radiobutton4" type="radio" name="sushi" value="" />Breakfast, lunch, AND dinner</label>
</fieldset>
<h3>I have never tried...</h3>
<fieldset>
<label for="checkbox1" class="field-checkbox"><input id="checkbox1" type="checkbox" name="never[]" value="Spelunking" />Spelunking</label>
<label for="checkbox2" class="field-checkbox"><input id="checkbox2" type="checkbox" name="never[]" value="Exotic Travel" />Exotic Travel</label>
<label for="checkbox3" class="field-checkbox"><input id="checkbox3" type="checkbox" name="never[]" value="Scuba Diving" />Scuba Diving</label>
</fieldset>
<h3>Comments</h3>
<fieldset>
<label class="field-routine">What's your morning routine?<br /><textarea name="routine" cols="40" rows="5"></textarea></label>
</fieldset>
<h3>Send</h3>
<fieldset>
<label>Click "Send" to post your information</label>
<input type="submit" name="submit" value="Send" />
<input type="hidden" name="submitted" value="true" />
</fieldset>
</form>
Session 2: The next step in completing our form project involves creating the logic necessary to process the data entered into the form by the user. The code I’ll be demonstrating today will perform several tasks using PHP.
1. We’ll use a PHP include in the form to perform the tasks
2. The first tasks performed will be to validate the data entered into the form
3. The data will be processed to prevent harmful header injection attacks
4. An email will be sent containing all the processed data from the form entry
5. Adequate feedback will be provided for the user as a response message
Take a look at this function that we will be using to validate form input for email addresses:
function is_valid_email($email) {
$result = true;
$pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .
'(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
if(!preg_match($pattern, $email)) {
$result = false;
}
return $result;
}