Advertisement
keihead

contact.php

Jul 17th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.77 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: Contact
  4. */
  5. ?>
  6.  
  7.  
  8. <?php
  9. //If the form is submitted
  10. if(isset($_POST['submitted'])) {
  11. //Check to see if the honeypot captcha field was filled in
  12. if(trim($_POST['checking']) !== '') {
  13.     $captchaError = true;
  14. } else {
  15.    
  16. //Check to make sure that the name field is not empty
  17.     if(trim($_POST['contactName']) === '') {
  18.         $nameError = 'You forgot to enter your name.';
  19.         $hasError = true;
  20.     } else {
  21.         $name = trim($_POST['contactName']);
  22.     }
  23.    
  24. //Check to make sure sure that a valid email address is submitted
  25.     if(trim($_POST['email']) === '')  {
  26.         $emailError = 'You forgot to enter your email address.';
  27.         $hasError = true;
  28.     } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
  29.         $emailError = 'You entered an invalid email address.';
  30.         $hasError = true;
  31.     } else {
  32.         $email = trim($_POST['email']);
  33.     }
  34.        
  35. //Check to make sure a subject was entered 
  36.     if(trim($_POST['subject']) === '') {
  37.         $subjectError = 'You forgot to enter a subject.';
  38.         $hasError = true;
  39.     } else {
  40.         if(function_exists('stripslashes')) {
  41.             $subject = stripslashes(trim($_POST['subject']));
  42.         } else {
  43.             $subject = trim($_POST['subject']);
  44.         }
  45.     }
  46.    
  47. //Check to make sure comments were entered 
  48.     if(trim($_POST['comments']) === '') {
  49.         $commentError = 'You forgot to enter your comments.';
  50.         $hasError = true;
  51.     } else {
  52.         if(function_exists('stripslashes')) {
  53.         $comments = stripslashes(trim($_POST['comments']));
  54.         } else {
  55.             $comments = trim($_POST['comments']);
  56.         }
  57.     }
  58.            
  59. //If there is no error, send the email
  60.     if(!isset($hasError)) {
  61.  
  62.         $emailTo = 'email@domain.com';
  63.         $subject = $subject;
  64.         $sendCopy = trim($_POST['sendCopy']);
  65.         $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments: $comments";
  66.         $headers = 'From: Your Name . <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
  67.            
  68.         mail($emailTo, $subject, $body, $headers);
  69.  
  70.         if($sendCopy == true) {
  71.             $headers = 'From: Your Name <noreply@somedomain.com>';
  72.             $body = "You recently requsted a copy of an email sent from domain.com Contact Form. \n\nPlease give us up to 24hours to repsond. \n\nName: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments: $comments";
  73.             mail($email, $subject, $body, $headers);
  74.         }
  75.  
  76.     $emailSent = true;
  77.  
  78.     }
  79. }
  80. } ?>
  81.  
  82. <?php get_header('header2'); ?>
  83.  
  84. <div id="container" class="clearfix">
  85.     <div id="left-content">
  86.     <?php if(isset($emailSent) && $emailSent == true) { ?>
  87.  
  88.         <div class="thanks">
  89.             <h1>Thanks, <?=$name;?></h1>
  90.             <p>Your email was successfully sent. I will be in touch soon.</p>
  91.         </div>
  92.  
  93.     <?php } else { ?>
  94.  
  95.         <?php if (have_posts()) : ?>
  96.        
  97.         <?php while (have_posts()) : the_post(); ?>
  98.             <h1><?php the_title(); ?></h1>
  99.      
  100.            
  101. <?php if(isset($hasError) || isset($captchaError)) { ?>
  102. <h2 class="error">There was an error submitting the form.</h2>
  103. <?php } ?>
  104.        
  105. <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
  106.  
  107. <ol class="forms">
  108. <li>
  109. <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" placeholder="Name" />                
  110. </br>
  111. <?php if($nameError != '') { ?>
  112. <span class="error"><?=$nameError;?></span>
  113. <?php } ?>
  114. </li>
  115.                    
  116. <li>
  117. <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="requiredField email" placeholder="Email" />
  118. </br>
  119. <?php if($emailError != '') { ?>
  120. <span class="error"><?=$emailError;?></span>
  121. <?php } ?>
  122. </li>
  123.                    
  124. <li>
  125. <input type="text" name="subject" id="subject" value="<?php if(isset($_POST['subject']))  echo $_POST['subject'];?>" class="requiredField subject" placeholder="Subject" />
  126. <br/>
  127. <?php if($subjectError != '') { ?>
  128. <span class="error"><?=$subjectError;?></span>
  129. <?php } ?>
  130. </li>
  131.                    
  132. <li class="textarea">
  133. <textarea name="comments" id="commentsText" rows="20" cols="30" class="requiredField" placeholder="Type your message here..."><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
  134. </br>
  135. <?php if($commentError != '') { ?>
  136. <span class="error"><?=$commentError;?></span>
  137. <?php } ?>
  138. </li>
  139.  
  140. <li class="inline">
  141. <input type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> /><label for="sendCopy">Send a copy of this email to yourself</label>
  142. </li>
  143.  
  144. <li class="buttons">
  145. <input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit">Send</button>
  146. </li>
  147.  
  148. </ol>
  149. </form>
  150.        
  151.             <?php endwhile; ?>
  152.         <?php endif; ?>
  153.     <?php } ?>
  154. </div><!-- #left-content -->
  155. </div><!-- #container -->
  156.  
  157. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement