Advertisement
Guest User

Untitled

a guest
Jun 21st, 2016
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.74 KB | None | 0 0
  1. <?php
  2.     $title = "Contact Us";
  3.     include 'header.php';
  4.  
  5.     if ( isset($_POST) && !empty($_POST) ) {   
  6.         // init error array
  7.         $send = true;
  8.         $errors = array();
  9.  
  10.         // check user name
  11.         if ( !empty($_POST['username']) ) {
  12.             $username = $_POST['username'];
  13.             $pattern = "/^[a-z ,.'-]+$/i";
  14.             if ( preg_match($pattern, $username) ) {
  15.                 $username = $_POST['username'];
  16.             } else {
  17.                 $errors[] = "name";
  18.             }
  19.         } else {
  20.             $errors[] = "name";
  21.         }
  22.  
  23.         // check email
  24.         if ( !empty($_POST['email']) ) {
  25.             $userEmail = $_POST['email'];
  26.             $pattern = "/^[\w&]([\w\-\.'&]*)@([\w\-\.]*)(\.[a-z]{2,6}(\.[a-z]{2}){0,2})$/i";
  27.             if ( preg_match($pattern, $userEmail) ) {
  28.                 $userEmail = $_POST['email'];
  29.             } else {
  30.                 $errors[] = 'email';
  31.             }
  32.         } else {
  33.             $errors[] = "email";
  34.         }
  35.  
  36.         // checking subject
  37.         if ( !empty($_POST['subject']) ) {
  38.             $subjectMessage = $_POST['username'] . " writes about: " . $_POST['subject'];
  39.         } else {
  40.             $errors[] = "subject";
  41.         }
  42.  
  43.         // checking message
  44.         if ( !empty($_POST['msg-body']) ) {
  45.             $message = $_POST['msg-body'];
  46.         } else {
  47.             $errors[] = "msg";
  48.         }
  49.     }
  50.  
  51.     if ( empty($errors) && isset($send) ) {
  52.         $from = "From: ".$username;
  53.         $to = "email@domain.com";
  54.  
  55.         $mailMessage =  "Message from contact form of xxx webpage. \n".
  56.                     "Sent by $username \n".
  57.                     "User writes following: \n".
  58.                     $message;
  59.  
  60.         $success = mail($to, $subjectMessage, $mailMessage, $from);
  61.     }
  62.  
  63. ?>
  64.    
  65.     <main class="contact">
  66.         <p>Whether you want to enquire about a quote, find out more about a package or simply say hello – we’d love to hear from you. Fill out the form below and we’ll do our best to get back to you as soon as we can!</p>
  67.  
  68.         <?php if ( !empty($errors) ) { ?>
  69.             <p class="notification">Specified fields are invalid. Please ensure that information is correct and try again.</p> 
  70.         <?php } ?>
  71.  
  72.         <?php if ( $success == true ) { ?>
  73.             <p class="notification">Thank you for your message. We will contact you as soon as possible!</p>
  74.         <?php } ?>
  75.  
  76.         <form action="contact.php" id="contact-form" method="POST" novalidate>
  77.  
  78.             <div class="form-part <?php if( isset($send) && in_array( "name", $errors ) ) echo "ui-state-invalid" ?>">
  79.                 <label for="name">Name</label>
  80.                 <input type="text"
  81.                         id="name"
  82.                         name="username"
  83.                         required="required"
  84.                         data-pattern="plainText"
  85.                         value="<?php if ( isset($send) && $success === false ) echo $username?>">
  86.             </div>
  87.             <div class="form-part <?php if( isset($send) && in_array( "email", $errors ) ) echo "ui-state-invalid" ?>">
  88.                 <label for="email">E-mail</label>
  89.                 <input type="email"
  90.                         id="email"
  91.                         name="email"
  92.                         required="required"
  93.                         data-pattern="email"
  94.                         value="<?php if ( isset($send) && $success === false ) echo $userEmail?>">
  95.             </div>
  96.  
  97.             <div class="form-part <?php if( isset($send) && in_array( "subject", $errors ) ) echo "ui-state-invalid" ?>">
  98.                 <label for="theme">Subject</label>
  99.                 <input type="text"
  100.                         id="theme"
  101.                         name="subject"
  102.                         required="required"
  103.                         data-pattern="plainText"
  104.                         value="<?php if ( isset($send) && $success === false ) echo $subject?>">
  105.             </div>
  106.             <div class="form-part msg-container <?php if( isset($send) && in_array( "msg", $errors ) ) echo "ui-state-invalid" ?>">
  107.                 <label for="message">message</label>
  108.                 <textarea name="msg-body"
  109.                             id="message"
  110.                             placeholder="Drop us a message"
  111.                             required="required"
  112.                             data-pattern="plainText"><?php if ( isset($send) && $success === false ) echo $message?></textarea>
  113.             </div>
  114.  
  115.             <input type="submit" class='button'>
  116.         </form>
  117.     </main>
  118.  
  119.     <?php
  120.         include 'footer.php';
  121.     ?>
  122.  
  123.     <script src='js/jquery.min.js'></script>
  124.     <script src='js/misc.js'></script>
  125.  
  126. </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement