nefi_c

dealer ajax search template

Jan 24th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <script type="text/javascript">
  2. jQuery(document).ready(function($) {
  3.  
  4.     dealerResults = function () {
  5.         var county = $('#dealer-county').val(),
  6.             town = $('#dealer-town').val(),
  7.             wp_nonce = '<?php echo wp_create_nonce("dealer_search");?>';
  8.         if(county != 0 && town != 0){
  9.             $.ajax({
  10.                 type: 'POST',
  11.                 url: '<?php echo admin_url('admin-ajax.php'); ?>',
  12.                 data: { varCounty : county, varTown : town, action: 'dealer_search', nonce: wp_nonce },
  13.                 success: function(data){
  14.                     $(".dealer-wrapper").remove();
  15.                     $("#dealer-results").append(data);     
  16.                 }
  17.             });
  18.         }
  19.     }
  20.    
  21.     var dealer_town = $('#dealer-town').clone(true);
  22.     $('#dealer-county').change(function(){
  23.         var selected_county = $('option:selected',this).val().replace(/ /g,"-");
  24.         if(selected_county != 0){
  25.             var selectedTown = dealer_town.clone(true).find('.default,.'+selected_county);
  26.             $('#dealer-town').removeAttr('disabled');
  27.             $('#dealer-town').empty().append(selectedTown);
  28.         } else {
  29.             $('#dealer-town').attr('disabled','disabled');
  30.         }
  31.     });
  32.    
  33.     $('#dealer-search').submit(function(e){ // Fire this function some how with the form submission.
  34.         e.preventDefault();
  35.         dealerResults();
  36.     });
  37.  
  38. });
  39. </script>
  40.  
  41. <?php
  42.     global $wpdb;
  43.     $all_dealer_region = $dealer_county = $dealer_town = Array();
  44.     $dealer_data = $wpdb->get_results( "SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE meta_key = 'dealer_county' OR meta_key = 'dealer_town'", ARRAY_A);
  45.    
  46.     // get all county base on meta_key and associate it with town
  47.     foreach($dealer_data as $d){
  48.       if($d['meta_key'] == 'dealer_county'){
  49.         if(!is_array($all_dealer_region[$d['meta_value']]))
  50.             $all_dealer_region[$d['meta_value']] = array();
  51.         $dealer_county[$d['post_id']] = $d['meta_value'];
  52.       }
  53.       if($d['meta_key'] == 'dealer_town'){
  54.         $dealer_town[$d['post_id']] = $d['meta_value'];
  55.       }
  56.     }
  57.     foreach($dealer_town as $key => $value){
  58.         if( !in_array($value, $all_dealer_region[$dealer_county[$key]]) ){
  59.             $all_dealer_region[$dealer_county[$key]][] = $value;
  60.         }
  61.     }
  62. ?>
  63. <div id="dealer-filter">
  64.     <form id="dealer-search" name="dealer-search">
  65.         <select name="dealer-county" id="dealer-county">
  66.             <option class="default" value="0">Choose County</option>
  67.             <?php foreach($all_dealer_region as $key => $value){
  68.                 echo "<option value=\"$key\">$key</option>";
  69.             } ?>
  70.         </select>
  71.         <select name="dealer-town" id="dealer-town" class="input-xlarge" disabled="disabled">
  72.             <option class="default" value="0">Choose Town</option>
  73.             <?php foreach($all_dealer_region as $key => $value){
  74.                     usort($value, 'dealer_sort');
  75.                     $county_class = str_replace(' ','-',$key);
  76.                     foreach($value as $town){
  77.                         echo "<option class=\"$county_class\" value=\"$town\">$town</option>";
  78.                     }
  79.             } ?>
  80.         </select>
  81.         <button type="submit" class="btn"><i class="icon-search"></i> Find dealers</button>
  82.     <form>
  83. </div>
  84.  
  85. <div id="dealer-results">
  86.     <!-- MY DEALER RESULTS WILL APPEAR HERE! -->
  87. </div>
Advertisement
Add Comment
Please, Sign In to add comment