Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2015
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.97 KB | None | 0 0
  1. <?php
  2.  
  3.     define( 'MAIL_TO',   'user@example.com' );
  4.     define( 'MAIL_FROM', 'info@example.com' );
  5.     define( 'SUBJECT',   'Website Feedback' );
  6.  
  7.     // Set default values and define what can be accepted via post
  8.     $aryData = array_fill_keys( [ 'Name', 'Email', 'Discourse' ], '' );
  9.     $aryData['Options'] = [];
  10.    
  11.     $aryErr = [];
  12.    
  13.     $bPosted = ( count( $_POST ) > 0 );
  14.     $bValidated = false;
  15.    
  16.    
  17.     function clean_string( $string, $bad = [ 'content-type', 'bcc:', 'to:', 'cc:', 'href' ] ) {
  18.         return str_replace( $bad, '', $string );
  19.     }
  20.    
  21.     if ( $bPosted ) {
  22.        
  23.         // Move POST values into main Data array
  24.         foreach( $_POST as $key => $val ) {
  25.             if ( preg_match( '/^(txt|drp|hid|chk|rad)([a-zA-Z0-9_-]+)$/', $key, $regs ) && array_key_exists( $regs[2], $aryData ) ) {
  26.                 $aryData[ $regs[2] ] = ( is_string( $val ) ) ? trim( $val ) : $val;
  27.             }
  28.         }
  29.  
  30.         // Validate Information        
  31.         if ( $aryData['Name'] == '' ) {
  32.             $aryErr['Name'] = 'Name is required';
  33.         }
  34.         elseif ( !preg_match( '/^[a-z .\'-]+$/i', $aryData['Name'] ) ) {
  35.             $aryErr['Name'] = 'Name contains invalid characters';
  36.         }
  37.        
  38.         if ( $aryData['Email'] == '' ) {
  39.             $aryErr['Email'] = 'Email is required';
  40.         }
  41.         elseif ( !filter_var( $aryData['Email'], FILTER_VALIDATE_EMAIL ) ) {
  42.             $aryErr['Email'] = 'Invalid Email address';
  43.         }
  44.            
  45.         if ( $aryData['Discourse'] == '' ) {
  46.             $aryErr['Discourse'] = 'Please provide some comments';    
  47.         }
  48.        
  49.         $bValidated = ( count( $aryErr ) == 0 );
  50.        
  51.     }
  52.    
  53.     if ( $bPosted && $bValidated ) {
  54.        
  55.         $aryMsg = [];
  56.         $aryMsg[] = 'Form details below:';
  57.         $aryMsg[] = "\n";
  58.         $aryMsg[] = 'Name: ' . clean_string( $aryData['Name' ] );
  59.         $aryMsg[] = 'Email: ' . clean_string( $aryData['Email'] );
  60.         $aryMsg[] = 'Comments: ' .clean_string( $aryData['Discourse'] );
  61.         $aryMsg[] = 'Options: ';
  62.         if ( count( $aryData['Options'] ) == 0 ) {
  63.             $aryMsg[] = '     (Non Selected)';
  64.         }
  65.         else {
  66.             foreach( $aryData['Options'] as $val ) {
  67.                 $aryMsg[] = '     ' . $val;
  68.             }
  69.         }
  70.        
  71.         $strHeaders = 'From: ' . MAIL_FROM . "\r\n" .
  72.                                     'Reply-To: ' . MAIL_FROM . "\r\n" .
  73.                                     'X-Mailer: PHP/' . phpversion();
  74.                                    
  75.         // Commented out for testing...  
  76.        
  77. //        if ( !mail( MAIL_TO, SUBJECT, implode( "\n", $aryMsg ), $strHeaders ) ) {
  78. //            die( 'There was an unexpected error sending contact form' );
  79. //        }
  80.  
  81.         // For Debug Only
  82.         echo '<div style="white-space: pre; border: #000 1px solid; background-color: #ccc; font-family: monospace; padding: 20px;">';
  83.         echo 'To: ' . MAIL_TO . "\n" . $strHeaders ;
  84.         echo '<hr>';
  85.         echo implode( "\n", $aryMsg );
  86.         echo '</div>';
  87.  
  88.     }
  89.  
  90.    
  91.     function echoTextInput( &$data, $field, $label = '', $placeholder = '', $extra = '' ) {
  92.         if ( $label == '' ) { $label = $field; }
  93.         if ( $placeholder == '' ) { $placeholder = $label; }
  94.        
  95.         echo '<label for="txt', $field, '">', htmlspecialchars( $label, ENT_QUOTES ), "</label>\n";
  96.         echo '<input name="txt', $field, '" id="txt', $field, '" class="txtinput" ';
  97.         echo   'placeholder="', htmlspecialchars( $placeholder, ENT_QUOTES ), '" ';
  98.         echo   'value ="' . htmlspecialchars( $data[ $field ], ENT_QUOTES ), '" ', $extra;
  99.         if ( strpos( $extra, 'type=' ) === false ) {
  100.             echo ' type="text" ';
  101.         }
  102.         echo " >\n";
  103.     }
  104.    
  105.     function echoCheckboxInput( &$data, $field, $value = '', $label = '', $extra = '' ) {
  106.         static $ids = [];
  107.         if ( !array_key_exists( $field, $ids ) ) { $ids[ $field ] = 0; }
  108.         $ids[ $field ]++;
  109.  
  110.         if ( $value == '' ) { $value = $ids[ $field ]; }
  111.         if ( $label == '' ) { $label = $field; }
  112.                
  113.         echo "<div>\n";
  114.         echo '<label for="chk', $field, '-', $ids[ $field ], '" class="tag">', htmlspecialchars( $label, ENT_QUOTES ), "</label>\n";
  115.         echo '<input type="checkbox" id="chk', $field, '-', $ids[ $field ], '" ';
  116.         echo   'name="chk', $field, '[]" class="regular-checkbox big-checkbox" ';
  117.         echo   'value="', htmlspecialchars( $value, ENT_QUOTES ), '" ';
  118.         if ( in_array( $value, $data[ $field ] ) ) {
  119.             echo ' checked="checked" ';
  120.         }
  121.         echo ">\n<div>\n";
  122.     }
  123.    
  124. ?><html>
  125. <head><title>Contact</title></head>
  126. <body>
  127.     <h2>Contact</h2>
  128.    
  129.     <?php if ( $bPosted && $bValidated ) { ?>
  130.        
  131.         <p>Thank you for contacting us!</p>
  132.    
  133.     <?php } else { ?>
  134.    
  135.         <?php if ( $bPosted && !$bValidated ) { ?>
  136.             <div class="errors">
  137.                 Check the following and try again:
  138.                 <ul><li><?php echo implode( '</li><li>', $aryErr ); ?></li></ul>
  139.             </div>
  140.         <?php } ?>
  141.    
  142.         <section class="contact">  
  143.             <form id="contact" action="" method="post">
  144.                 <fieldset title="About you">
  145.  
  146.                     <legend><span>About you</span></legend>
  147.  
  148.                     <?php echoTextInput( $aryData, 'Name', 'Name', 'Your Name', 'tabindex="10" required' ); ?>
  149.                    
  150.                     <?php echoTextInput( $aryData, 'Email', 'Email', 'Valid Email', 'tabindex="20" required type="email"' ); ?>
  151.  
  152.                 </fieldset>
  153.  
  154.                 <fieldset title="Your turn">
  155.                     <legend><span>Your turn</span></legend>
  156.                    
  157.                     <p>What, exactly then, are you after?</p>
  158.                    
  159.                     <?php echoCheckboxInput( $aryData, 'Options', 'salvation', 'Salvation' ); ?>
  160.  
  161.                     <?php echoCheckboxInput( $aryData, 'Options', 'question', 'Question' ); ?>
  162.  
  163.                     <?php echoCheckboxInput( $aryData, 'Options', 'other', 'Other' ); ?>
  164.                                
  165.                     <label for="txtDiscourse">Write your comments here</label>
  166.                     <textarea id="txtDiscourse" name="txtDiscourse" tabindex="30" class="txtinput" rows="7" placeholder="This is where your thoughts go"><?php echo htmlspecialchars( $aryData['Discourse'], ENT_QUOTES ); ?></textarea>
  167.  
  168.                     <section id="buttons">
  169.                         <input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset">
  170.                         <input type="submit" name="submit" id="submitbtn" class="submitbtn" tabindex="40" value="Send away!">
  171.                         <br style="clear:both;">
  172.                     </section>
  173.                 </fieldset>
  174.             </form>
  175.         </section>
  176.    
  177.     <?php } // END: else: if( Posted and Valid ) ?>
  178. </body>
  179. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement