Advertisement
w0lfiesmith

AJAX demo, customers template

Feb 9th, 2012
8,771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Template Name: Customers
  4. */
  5.  
  6.  
  7. get_header(); ?>
  8.  
  9.         <div id="primary">
  10.             <div id="content" role="main">
  11.  
  12.                 <?php while ( have_posts() ) : the_post(); ?>
  13.  
  14.                     <?php get_template_part( 'content', 'page' ); ?>
  15.                    
  16.                     <?php
  17.                    
  18.                     if (is_user_logged_in()):?>
  19.                    
  20.                         <form type="post" action="" id="newCustomerForm">
  21.                            
  22.                             <label for="name">Name:</label>
  23.                             <input name="name" type="text" />
  24.                            
  25.                             <label for="email">Email:</label>
  26.                             <input name="email" type="text" />
  27.                            
  28.                             <label for="phone">Phone:</label>
  29.                             <input name="phone" type="text" />
  30.                            
  31.                             <label for="address">Address:</label>
  32.                             <input name="address" type="text" />
  33.                            
  34.                             <input type="hidden" name="action" value="addCustomer"/>
  35.                             <input type="submit">
  36.                         </form>
  37.                         <br/><br/>
  38.                         <div id="feedback"></div>
  39.                         <br/><br/>
  40.                        
  41.                    
  42.                     <?php
  43.                         global $wpdb;
  44.                         $customers = $wpdb->get_results("SELECT * FROM customers;");
  45.                                            
  46.                         echo "<table>";
  47.                         foreach($customers as $customer){
  48.                             echo "<tr>";
  49.                             echo "<td>".$customer->name."</td>";
  50.                             echo "<td>".$customer->email."</td>";
  51.                             echo "<td>".$customer->phone."</td>";
  52.                             echo "<td>".$customer->address."</td>";
  53.                             echo "</tr>";
  54.                         }
  55.                         echo "</table>";
  56.                     else:
  57.                         echo "Sorry, only registered users can view this information";
  58.                     endif;                     
  59.                    
  60.                     ?>
  61.                    
  62.                     <script type="text/javascript">
  63.                         jQuery('#newCustomerForm').submit(ajaxSubmit);
  64.                        
  65.                         function ajaxSubmit(){
  66.                            
  67.                             var newCustomerForm = jQuery(this).serialize();
  68.                            
  69.                             jQuery.ajax({
  70.                                 type:"POST",
  71.                                 url: "/wp-admin/admin-ajax.php",
  72.                                 data: newCustomerForm,
  73.                                 success:function(data){
  74.                                     jQuery("#feedback").html(data);
  75.                                 },
  76.                                 error: function(errorThrown){
  77.                                     alert(errorThrown);
  78.                                 }  
  79.                             });
  80.                            
  81.                             return false;
  82.                         }
  83.                     </script>
  84.                    
  85.  
  86.  
  87.                 <?php endwhile; // end of the loop. ?>
  88.  
  89.             </div><!-- #content -->
  90.         </div><!-- #primary -->
  91.  
  92. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement