tag print ''; } function display_form($defaults) { global $subject_options; ?>
} function validate_form($data) { $errors = array(); /* ASSIGNMENT: replace this comment block with code to enforce the following validation rules: -- email field must look like an email address -- email field must be non-empty -- optional phone field, if not empty, must contain exactly 10 digits -- comments field must be non-empty -- comments field must not exceed 800 characters in length -- subject field must be non-empty -- subject field must be a key found in the array $subject_options If a field value is just whitespace, then it's considered empty. Use trim() to remove trailing and leading whitespace, then examine it. To validate the 'subject' form variable, use array_key_exists() (see http://php.net/manual/en/function.array-key-exists.php) For each form element whose value fails a test, add an appropriate error message to the $errors array. Use this regex for the email validation: '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i' Save this file as week3.php and hand it in. */ return $errors; } function process($data) { echo "Thank you for your submission. Your comments are genuinely appreciated.
"; } // end function definitions /////////////////////////// // begin form-handling logic /////////////////////////// $subject_options = array( ''=>'[select one]', 'rant'=>'rant', 'praise'=>'praise', 'question'=>'question'); if ($_POST) { $errors = validate_form($_POST); if ($errors) { echo "Sorry, we are unable to process your submission because:
Please correct your form below and re-submit it
"; display_form($_POST); } else { process($_POST); } } else { $defaults = array('subject'=>'','comments'=>'','phone'=>'','email'=>''); display_form($defaults); }