Advertisement
menjxo

zip code search

Sep 20th, 2017
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Zip Code Real-time Search
  4. Description: Real-time zip code searc from database.
  5. Version: 1.1
  6.  
  7. */
  8.  
  9. add_action('init',function(){
  10.     function live_search_function(){
  11.         $content = ' <div id="livesearch_box">
  12.                
  13.                     <label id="zipcode_label">Zip Code:</label> <input type="text" id="zip_code" name="zip_code">
  14.                     <label id="flatrate_label">Flat Rate:</label> <input type="text" id="flat_rate" name="flat_rate" readonly>
  15.                     <input type="submit" value="Book Now" id="booknow-ls">
  16.         </div>';
  17.         return $content;
  18.     }
  19.     add_shortcode('live_search', 'live_search_function');
  20. });
  21.  
  22.  
  23. add_action('wp_head', 'myplugin_ajaxurl');
  24. function myplugin_ajaxurl() {
  25.  
  26.    echo '<script type="text/javascript">
  27.           var ajaxurl = "' . admin_url('admin-ajax.php') . '";
  28.         </script>';
  29. }
  30.  
  31. add_action('wp_footer', 'my_action_javascript');  // Write our JS below here
  32. function my_action_javascript() { ?>
  33.     <?php wp_enqueue_script('jquery'); ?>
  34.     <!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> -->
  35.    
  36.     <script type="text/javascript" >
  37.     jQuery( document ).ready(function() {
  38.         jQuery( "#zip_code" ).keyup(search_key_press);
  39.     });
  40.  
  41.    
  42.    
  43.     function search_key_press(){
  44.             code = jQuery("#zip_code").val();
  45.             var data = {
  46.                 'action': 'my_action_search_key_press',
  47.                 'code': code
  48.             };
  49.             jQuery.ajax({
  50.                 url: ajaxurl,
  51.                 method: "POST",
  52.                 data: data,
  53.                 dataType: "json",
  54.                 success: function(result){
  55.                     if(result!="" || result!=null){
  56.                         jQuery("#flat_rate").val(result);
  57.                     }
  58.  
  59.                 }
  60.             });
  61.            
  62.     }
  63.  
  64.     </script> <?php
  65. }
  66.  
  67. add_action( 'wp_ajax_my_action_search_key_press', 'action_search_key_press_callback' );
  68.  
  69. function action_search_key_press_callback() {
  70.     global $wpdb;
  71.     $code =  $_POST['code'];
  72.     $table = "wp_zip";
  73.     $sql = "SELECT flat_rate FROM wp_zip WHERE zip_code = '$code'";
  74.     $rate = $wpdb->get_var($sql);
  75.     echo json_encode($rate);
  76.     wp_die();
  77.  
  78.     // wildcard searc
  79.     // LIKE '%$code%'";
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement