Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Zip Code Real-time Search
- Description: Real-time zip code searc from database.
- Version: 1.1
- */
- add_action('init',function(){
- function live_search_function(){
- $content = ' <div id="livesearch_box">
- <label id="zipcode_label">Zip Code:</label> <input type="text" id="zip_code" name="zip_code">
- <label id="flatrate_label">Flat Rate:</label> <input type="text" id="flat_rate" name="flat_rate" readonly>
- <input type="submit" value="Book Now" id="booknow-ls">
- </div>';
- return $content;
- }
- add_shortcode('live_search', 'live_search_function');
- });
- add_action('wp_head', 'myplugin_ajaxurl');
- function myplugin_ajaxurl() {
- echo '<script type="text/javascript">
- var ajaxurl = "' . admin_url('admin-ajax.php') . '";
- </script>';
- }
- add_action('wp_footer', 'my_action_javascript'); // Write our JS below here
- function my_action_javascript() { ?>
- <?php wp_enqueue_script('jquery'); ?>
- <!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> -->
- <script type="text/javascript" >
- jQuery( document ).ready(function() {
- jQuery( "#zip_code" ).keyup(search_key_press);
- });
- function search_key_press(){
- code = jQuery("#zip_code").val();
- var data = {
- 'action': 'my_action_search_key_press',
- 'code': code
- };
- jQuery.ajax({
- url: ajaxurl,
- method: "POST",
- data: data,
- dataType: "json",
- success: function(result){
- if(result!="" || result!=null){
- jQuery("#flat_rate").val(result);
- }
- }
- });
- }
- </script> <?php
- }
- add_action( 'wp_ajax_my_action_search_key_press', 'action_search_key_press_callback' );
- function action_search_key_press_callback() {
- global $wpdb;
- $code = $_POST['code'];
- $table = "wp_zip";
- $sql = "SELECT flat_rate FROM wp_zip WHERE zip_code = '$code'";
- $rate = $wpdb->get_var($sql);
- echo json_encode($rate);
- wp_die();
- // wildcard searc
- // LIKE '%$code%'";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement