1

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?

1
  • 1: Which part of your php code accepts a JSON request and can reply with JSON response ? 2: in your source property of autocomplete you can specify a callback function like this function(request,response){} from jquery docs: jqueryui.com/autocomplete/#remote-jsonp Commented Jun 25, 2013 at 15:48

1 Answer 1

1

After I encode the strings with json, I could fix this issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.