All pastes #590749 Raw Edit

Anonymous

public text v1 · immutable
#590749 ·published 2007-06-26 19:22 UTC
rendered paste body
<?php
$sweets = array('Sesame Seed Puff','Coconut Milk Gelatin Square',                  'Brown Sugar Cake','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_;
    foreach ($GLOBALS['sweets'] as $choice) { 
        print "<option>$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 (! in_array($_POST['order'], $GLOBALS['sweets'])) {
        $errors[  ] = 'Please choose a valid order.'; 
    }
    return $errors; 
}
?>