I a trying to add Jquery autocomplet to wordpress. I have a working jquery ajax search which get the users name.
I would like to do it with autocomplet. I notice that we need to use jason to passe data.
Can someone direct me to correct way to do this. Here is header.php
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"/></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"/></script>
Here is the javascript I try to implement.
$("#searchbox").keyup(function(e){
e.preventDefault();
var search_val=$("#searchbox").val();
$.ajax({
type:"POST",
url: "./wp-admin/admin-ajax.php",
data: {
action:'wpay_search',
user_name:search_val
},
success:function(data){
//if(data==""){
// return false;
//}
$('#search_result').html(data);
}
});
});
Here is the php code in the wordpress plugin
function wpay_search() {
global $wpdb;
$name=$_POST['user_name'];
$employee=$wpdb->get_results("SELECT First_Name FROM table_list WHERE First_name LIKE '$name%' ");
foreach($employee as $key=> $value){
echo '<ul>';
echo '<li>'.$value->First_Name;'</li>';
echo '</ul>';
// echo $value->post_content;
}
//wp_reset_query();
die();
} // end theme_custom_handler
add_action( 'wp_ajax_wpay_search', 'wpay_search' );
add_action( 'wp_ajax_nopriv_wpay_search', 'wpay_search' );
In the jquery autocomplet example I found this
$( "#birds" ).autocomplete({
source: "search.php",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
Here I don't have clear idea how to passe data into source since I use wordpress.
How to do this?