dimanche 28 décembre 2014

Custom searchfield autocomplete


I have tried to fire an autocomplete on a customized search field but to no avail. I have used some codes from help sites but I really do not know what I am doing wrong. Nothing happens when I type a letter on to the search field. it is supposed to autocomplete with results from database table. This is my function defined in the functions.php


function department_action_callback(){


$mydb = new wpdb('XXXXX','XXXXX','XXXXX','localhost');


$facility_name=$_GET['facility_name'];


$facilities=$mydb->get_results("SELECT facility_name from facility where facility_name like '%".$facility_name."'");


$data = "";



echo '<ul>';

foreach($facilities as $dis)

{

echo '<li>'.$dis->facility_name.'</li>';

}

echo '</ul>' ;

die();


}


add_action('wp_ajax_department_action','department_action_callback' );


add_action('wp_ajax_nopriv_department_action','department_action_callback' );


This is the custom template file where I have defined the JQUery


jQuery(document).ready(function($) { jQuery('#facility_name').keyup(function() { cid=jQuery(this).attr('val'); var ajaxurl=""; var data ={ action: "department_action", facility_name:facility_id }; $.post(ajaxurl, data, function (response){ $('#key').html(response); }); }); });

<div id="content" class="site-content" role="main">


<?php while ( have_posts() ) : the_post(); ?>


<?php get_template_part( 'content', 'page' ); ?>


// search field defined here



<?php if ( comments_open() || '0' != get_comments_number() ) : ?>


<?php endif; ?>


<?php endwhile; // end of the loop. ?>


</div><!-- #content .site-content -->




Aucun commentaire:

Enregistrer un commentaire