Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.42 KB | None | 0 0
  1. <?php
  2.  
  3. // In one place, set a variable that is readable to use anywhere
  4. // you need to know this. It is cleaner code later, and better readable
  5.  
  6. $bIsPosted = ( count( $_POST ) > 0 );
  7.  
  8. // Set a variable to contain any errors, and one to say if there were no errors (default to true)
  9. $aErrors   = [];
  10. $bNoErrors = true;
  11.  
  12. // Initialize variables that will display in the form. Do this so they DO exist before form
  13. // is submitted, and you can set defaults to display in form (ie. default the country)
  14.  
  15. $name    = '';
  16. $email   = '';
  17. $country = 'US';
  18.  
  19. if ( $bIsPosted ) {
  20.  
  21.     // This whole block only gets done if the form was submitted
  22.     // For simplicity here, assuming it was a proper form, with all needed
  23.     // inputs included
  24.  
  25.     $name  = trim( $_POST['name'] );
  26.     $email = trim( $_POST['email'] );
  27.     // and your others....
  28.  
  29.     // Validate Values FOR SIMPLICITY, Make sure all error messages are using needed
  30.     // HTML entities ready to display
  31.     if ( $name == '' ) {
  32.         $aErrors[] = 'Name is required';
  33.     }
  34.  
  35.     if ( $email == '' ) {
  36.         $aErrors[] = 'Email is required';
  37.     } elseif ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
  38.         $aErrors[] = 'Email address is not valid';
  39.     }
  40.  
  41.     // and other validation as needed.
  42.  
  43.     // Set the variable to say if there are errors
  44.     $bNoErrors = ( count( $aErrors ) == 0 );
  45.  
  46. }
  47.  
  48. if ( $bIsPosted && $bNoErrors ) {
  49.  
  50.     // Code to build email and send it
  51.  
  52. } else {
  53.  
  54.     // Setup values for things such as select inputs in the form
  55.     // Done here as they are not needed if the email was sent...
  56.  
  57.     $aCountries = [
  58.         'US' => 'United States',
  59.         'CA' => 'Canada',
  60.         /* ... Others.... */
  61.     ];
  62.  
  63. }
  64.  
  65. // At this point, all processing is done, now we output to the visitor,
  66. // here are some functions to make the output code cleaner.
  67.  
  68. // This is a alias function to make output code cleaner later....
  69. function hsc( $s ) {
  70.     return htmlspecialchars( $s, ENT_QUOTES );
  71. }
  72.  
  73. function outputOptions( $options, $default ) {
  74.     foreach ( $options as $key => $val ) {
  75.         if ( $key == $default ) {
  76.             echo '<option value="', $key, '" selected="selected">', hsc( $val ), '</option>';
  77.         } else {
  78.             echo '<option value="', $key, '">', hsc( $val ), '</option>';
  79.         }
  80.     }
  81. }
  82.  
  83. ?>
  84. <html>
  85.  
  86. <!-- all code up to the form area.... -->
  87.  
  88. <?php if ( $bIsPosted && $bNoErrors ) { ?>
  89.  
  90.     <div class="success">
  91.         Your form was submitted sucessfully!
  92.     </div>
  93.  
  94. <?php } else {  // Not submitted or has errors?>
  95.  
  96.     <?php if ( ! $bNoErrors ) { // There WERE errors ?>
  97.         <div class="errors">
  98.             Please check the following and try again:
  99.             <ul>
  100.                 <li><?= implode( '</li><li>', $aErrors ); ?></li>
  101.             </ul>
  102.         </div>
  103.     <?php } // end display errors ?>
  104.  
  105.     <form action="" method="post">
  106.         <!-- kept basic here, need to add labels and formatting -->
  107.  
  108.         <input type="text" name="name" value="<?= hsc( $name ) ?>">
  109.  
  110.         <input type="email" name="email" value="<?= hsc( $email ) ?>">
  111.  
  112.         <select name="country">
  113.             <?php outputOptions( $aCountries, $country ); ?>
  114.         </select>
  115.  
  116.         <input type="submit" name="submit" value="Send Message">
  117.  
  118.     </form>
  119. <?php } // end else posted and no errors ?>
  120.  
  121. <!-- all code after the form area.... -->
  122.  
  123. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement