Advertisement
deepdoank

My Form

Aug 21st, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.14 KB | None | 0 0
  1. <?php
  2. /*
  3. Template Name: KKJ
  4. */
  5. ?>
  6.  
  7. <?php
  8.  
  9. $formTitleField = 'Formulir-KKJ'; // change this as needed
  10. $formTitleField = 'form_title'; // change this as needed
  11. if (is_array($_POST) && !empty($_POST)) {
  12.     $title = isset($_POST[$formTitleField]) ?
  13.                    $_POST[$formTitleField] : 'Untitled';
  14.     $posted_data = array();
  15.     $uploaded_files = array();
  16.  
  17.     // Get posted values
  18.     foreach ($_POST as $key => $val) {
  19.         if ($key != $formTitleField) {
  20.             $posted_data[$key] = $val;
  21.         }
  22.     }
  23.  
  24.     // Get uploaded files
  25.     if (is_array($_FILES) && !empty($_FILES)) {
  26.         foreach ($_FILES as $key => $file) {
  27.             if (is_uploaded_file($file['tmp_name'])) {
  28.                 $posted_data[$key] = $file['name'];
  29.                 $uploaded_files[$key] = $file['tmp_name'];
  30.             }
  31.         }
  32.     }
  33.  
  34.     // Prepare data structure for call to hook
  35.     $data = (object) array(
  36.         'title' => $title,
  37.         'posted_data' => $posted_data,
  38.         'upload' => $_FILES['upload']['name'],
  39.         'uploaded_files' => array ( 'upload' => 'http://localhost/gbisawangan.org/assets/gbr/'));
  40.  
  41.     // Call hook to submit data
  42.     do_action_ref_array('cfdb_submit', array(&$data));
  43. }
  44.  
  45.  
  46. if(isset($_POST['submitted'])) {
  47.     if(trim($_POST['contactName']) === '') {
  48.         $nameError = 'Please enter your name.';
  49.         $hasError = true;
  50.     } else {
  51.         $name = trim($_POST['contactName']);
  52.     }
  53.  
  54.     if(trim($_POST['email']) === '')  {
  55.         $emailError = 'Please enter your email address.';
  56.         $hasError = true;
  57.     } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
  58.         $emailError = 'You entered an invalid email address.';
  59.         $hasError = true;
  60.     } else {
  61.         $email = trim($_POST['email']);
  62.     }
  63.  
  64.     if(trim($_POST['comments']) === '') {
  65.         $commentError = 'Please enter a message.';
  66.         $hasError = true;
  67.     } else {
  68.         if(function_exists('stripslashes')) {
  69.             $comments = stripslashes(trim($_POST['comments']));
  70.         } else {
  71.             $comments = trim($_POST['comments']);
  72.         }
  73.     }
  74.  
  75.     if(!isset($hasError)) {
  76.         $emailTo = get_option('tz_email');
  77.         if (!isset($emailTo) || ($emailTo == '') ){
  78.             $emailTo = get_option('admin_email');
  79.         }
  80.         $subject = '[PHP Snippets] From '.$name;
  81.         $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
  82.         $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
  83.  
  84.         wp_mail($emailTo, $subject, $body, $headers);
  85.         $emailSent = true;
  86.     }
  87.  
  88. } ?>
  89. <?php get_header(); ?>
  90.     <section class="container clearfix">
  91.         <div id="header" class="">
  92.             <header class="container page_info clearfix breadcrumb">
  93.                 <h1 class="regular bottom_line"><?php the_title(); ?></h1>
  94.             </header><!-- .entry-header -->
  95.         </div>
  96.         <div class="clear"></div>
  97.  
  98.         <div id="primary" class="site-content content clearfix">
  99.             <div id="content" role="main">
  100.             <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  101.             <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
  102.                     <div class="entry-content">
  103.                         <?php if(isset($emailSent) && $emailSent == true) { ?>
  104.                             <div class="thanks">
  105.                                 <p>Thanks, your email was sent successfully.</p>
  106.                             </div>
  107.                         <?php } else { ?>
  108.                             <?php the_content(); ?>
  109.                             <?php if(isset($hasError) || isset($captchaError)) { ?>
  110.                                 <p class="error">Sorry, an error occured.<p>
  111.                             <?php } ?>
  112.  
  113.                         <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
  114.                             <ul class="contactform">
  115.                             <li>
  116.                                 <label for="contactName">Name:</label>
  117.                                 <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
  118.                                 <?php if($nameError != '') { ?>
  119.                                     <span class="error"><?=$nameError;?></span>
  120.                                 <?php } ?>
  121.                             </li>
  122.  
  123.                             <li>
  124.                                 <label for="email">Email</label>
  125.                                 <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
  126.                                 <?php if($emailError != '') { ?>
  127.                                     <span class="error"><?=$emailError;?></span>
  128.                                 <?php } ?>
  129.                             </li>
  130. <li> <input type="file" name="upload"/> </li>
  131.                             <li><label for="commentsText">Message:</label>
  132.                                 <textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
  133.                                 <?php if($commentError != '') { ?>
  134.                                     <span class="error"><?=$commentError;?></span>
  135.                                 <?php } ?>
  136.                             </li>
  137.  
  138.                             <li>
  139.                                 <input type="submit">Send email</input>
  140.                             </li>
  141.                         </ul>
  142.                         <input type="hidden" name="submitted" id="submitted" value="true" />
  143.                     </form>
  144.                 <?php } ?>
  145.                 </div><!-- .entry-content -->
  146.             </div><!-- .post -->
  147.  
  148.                 <?php endwhile; endif; ?>
  149.                 <div class="clear padding10"></div>
  150.                
  151.                
  152.                
  153.             </div><!-- #content -->
  154.         </div><!-- #primary .site-content -->
  155.  
  156. <?php get_sidebar(); ?>
  157. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement