rendered paste body<?php
$sweets = array('puff' => 'Sesame Seed Puff', 'square' => 'Coconut Milk Gelatin Square',
'cake' => 'Brown Sugar Cake',
'ricemeat' => 'Sweet Rice and Meat');
// Logic to do the right thing based on // the hidden _submit_check parameter
if ($_POST['_submit_check']) {
// If validate_form( ) returns errors, pass them to show_form( )
if ($form_errors = validate_form( )) {
show_form($form_errors);
} else {
process_form( );
}
} else {
show_form( );
}
// Do something when the form is submitted
function process_form( ) {
print "You ordered a ". $_POST['order'];
}
// Display the form
function show_form($errors='') {
if($errors) {
print "<ul>";
foreach($errors as $error){
print "<li>$error</li>";
}
print "</ul>";
}
print<<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your Order: <select name="order">
_HTML_;
// $val is the option value, $choice is what's displayed
foreach ($GLOBALS['sweets'] as $val => $choice) {
print "<option value=\"$val\">$choice</option>\n";
}
print<<<_HTML_
</select>
<br/>
<input type="submit" value="Order">
<input type="hidden" name="_submit_check" value="1">
</form>
_HTML_;
}
// Check the form data
function validate_form( ) {
// Start with an empty array of error messages
$errors = array( );
if (! array_key_exists($_POST['order'], $GLOBALS['sweets'])) {
$errors[ ] = 'Please choose a valid order.';
}
return $errors;
}
?>