Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.33 KB | None | 0 0
  1. <?php
  2.  
  3. // Set email variables
  4. $email_to = 'youremail@address.com';
  5. $email_subject = 'Form submission';
  6.  
  7. // Set required fields
  8. $required_fields = array('fullname','email','comment');
  9.  
  10. // set error messages
  11. $error_messages = array(
  12.     'fullname' => 'Please enter a Name to proceed.',
  13.     'email' => 'Please enter a valid Email Address to continue.',
  14.     'comment' => 'Please enter your Message to continue.'
  15. );
  16.  
  17. // Set form status
  18. $form_complete = FALSE;
  19.  
  20. // configure validation array
  21. $validation = array();
  22.  
  23. // check form submittal
  24. if(!empty($_POST)) {
  25.     // Sanitise POST array
  26.     foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
  27.    
  28.     // Loop into required fields and make sure they match our needs
  29.     foreach($required_fields as $field) {      
  30.         // the field has been submitted?
  31.         if(!array_key_exists($field, $_POST)) array_push($validation, $field);
  32.        
  33.         // check there is information in the field?
  34.         if($_POST[$field] == '') array_push($validation, $field);
  35.        
  36.         // validate the email address supplied
  37.         if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
  38.     }
  39.    
  40.     // basic validation result
  41.     if(count($validation) == 0) {
  42.         // Prepare our content string
  43.         $email_content = 'New Website Comment: ' . "\n\n";
  44.        
  45.         // simple email content
  46.         foreach($_POST as $key => $value) {
  47.             if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
  48.         }
  49.        
  50.         // if validation passed ok then send the email
  51.         mail($email_to, $email_subject, $email_content);
  52.        
  53.         // Update form switch
  54.         $form_complete = TRUE;
  55.     }
  56. }
  57.  
  58. function validate_email_address($email = FALSE) {
  59.     return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
  60. }
  61.  
  62. function remove_email_injection($field = FALSE) {
  63.    return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
  64. }
  65. ?>
  66.  
  67. <head>
  68.     <title>Contact Form</title>
  69.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  70.    
  71.     <link href="contact/css/contactform.css" rel="stylesheet" type="text/css" />
  72.    
  73.     <script type="text/javascript">
  74.         var nameError = '<?php echo $error_messages['fullname']; ?>';
  75.         var emailError = '<?php echo $error_messages['email']; ?>';
  76.         var commentError = '<?php echo $error_messages['comment']; ?>';
  77.     </script>
  78.  
  79. </head>
  80.  
  81. <body>
  82.  
  83. <div id="formWrap">
  84. <div id="form">
  85. <?php if($form_complete === FALSE): ?>
  86. <form action="contact.php" method="post" id="comments_form">
  87.     <div class="row">
  88.     <div class="label">Your Name</div><!-- end label -->
  89.     <div class="input">
  90.    
  91.     <input type="text" id="fullname" class="detail" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" /><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?>    
  92.     </div><!-- end input -->
  93.     <div class="context">Example: John Doe or Jane Smith</div><!-- end context -->
  94.     </div><!-- end row -->
  95.    
  96.     <!-- END OF FORM 1 -->
  97.    
  98.     <div class="row">
  99.     <div class="label">Your E-Mail Address</div><!-- end label -->
  100.     <div class="input">
  101.    
  102.     <input type="text" id="email" class="detail" name="email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" /><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?>
  103.     </div><!-- end input -->
  104.     <div class="context">This is your personal information and we will not expose it to anybody.</div><!-- end context -->
  105.     </div><!-- end row -->
  106.    
  107.     <!-- END OF FORM 2 -->
  108.    
  109.     <div class="row">
  110.     <div class="label">Your Message</div><!-- end label -->
  111.     <div class="input">
  112.     <textarea id="comment" name="comment" class="mess"><?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?></textarea><?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?>
  113.     </div><!-- end input -->
  114.     </div><!-- end row -->
  115.    
  116.     <div class="submit">
  117.     <input type="submit" id="submit" name="submit" value="Send Message" />
  118.     </div><!-- end submit -->
  119.    
  120.     </form>
  121.     <?php else: ?>
  122. <p>Thank you for your Message!</p>
  123. <?php endif; ?>
  124.  
  125. </div><!-- end #form -->
  126. </div><!-- end formWrap -->
  127.  
  128. </body>
  129. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement