Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // In one place, set a variable that is readable to use anywhere
- // you need to know this. It is cleaner code later, and better readable
- $bIsPosted = ( count( $_POST ) > 0 );
- // Set a variable to contain any errors, and one to say if there were no errors (default to true)
- $aErrors = [];
- $bNoErrors = true;
- // Initialize variables that will display in the form. Do this so they DO exist before form
- // is submitted, and you can set defaults to display in form (ie. default the country)
- $name = '';
- $email = '';
- $country = 'US';
- if ( $bIsPosted ) {
- // This whole block only gets done if the form was submitted
- // For simplicity here, assuming it was a proper form, with all needed
- // inputs included
- $name = trim( $_POST['name'] );
- $email = trim( $_POST['email'] );
- // and your others....
- // Validate Values FOR SIMPLICITY, Make sure all error messages are using needed
- // HTML entities ready to display
- if ( $name == '' ) {
- $aErrors[] = 'Name is required';
- }
- if ( $email == '' ) {
- $aErrors[] = 'Email is required';
- } elseif ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
- $aErrors[] = 'Email address is not valid';
- }
- // and other validation as needed.
- // Set the variable to say if there are errors
- $bNoErrors = ( count( $aErrors ) == 0 );
- }
- if ( $bIsPosted && $bNoErrors ) {
- // Code to build email and send it
- } else {
- // Setup values for things such as select inputs in the form
- // Done here as they are not needed if the email was sent...
- $aCountries = [
- 'US' => 'United States',
- 'CA' => 'Canada',
- /* ... Others.... */
- ];
- }
- // At this point, all processing is done, now we output to the visitor,
- // here are some functions to make the output code cleaner.
- // This is a alias function to make output code cleaner later....
- function hsc( $s ) {
- return htmlspecialchars( $s, ENT_QUOTES );
- }
- function outputOptions( $options, $default ) {
- foreach ( $options as $key => $val ) {
- if ( $key == $default ) {
- echo '<option value="', $key, '" selected="selected">', hsc( $val ), '</option>';
- } else {
- echo '<option value="', $key, '">', hsc( $val ), '</option>';
- }
- }
- }
- ?>
- <html>
- <!-- all code up to the form area.... -->
- <?php if ( $bIsPosted && $bNoErrors ) { ?>
- <div class="success">
- Your form was submitted sucessfully!
- </div>
- <?php } else { // Not submitted or has errors?>
- <?php if ( ! $bNoErrors ) { // There WERE errors ?>
- <div class="errors">
- Please check the following and try again:
- <ul>
- <li><?= implode( '</li><li>', $aErrors ); ?></li>
- </ul>
- </div>
- <?php } // end display errors ?>
- <form action="" method="post">
- <!-- kept basic here, need to add labels and formatting -->
- <input type="text" name="name" value="<?= hsc( $name ) ?>">
- <input type="email" name="email" value="<?= hsc( $email ) ?>">
- <select name="country">
- <?php outputOptions( $aCountries, $country ); ?>
- </select>
- <input type="submit" name="submit" value="Send Message">
- </form>
- <?php } // end else posted and no errors ?>
- <!-- all code after the form area.... -->
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement