Jquery autocomplete not getting data from DB

need help to get blogs from wordpress DB using Jquery autocomplete.

i creating plugin, where one of the fields need to give to the user option to select some blog from the network.

Read More

the best solution will be to create Jquery autocomplete field. so when the user will start typing the Jquery autocomplete will get the results from DB.

maybe there is ready solution in the wordpress core and i miss something?

i have create field:

<form method="post">
<input id="mytest" type="text" name="mytest" style="width: 300px" />
 </form>

i have set Jquery code:

(function( $ ) {
    $(function() {
        $( "#mytest" ).autocomplete({
            source: "<? echo $campaign_plugin_url; ?>/autocomplete.php",
            minLength: 1 // how many character when typing to display auto complete
        });
    });

})( jQuery );

here is the code(in autocomplete.php) to get blogs from DB (this code was in wordpress core until 3.4):

function wp_ajax_autocomplete_site() { 

            global $wpdb;
            $like_escaped_term = strtolower( $_GET['term'] );
            $sites = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE ( domain LIKE %s OR path LIKE %s ) AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $like_escaped_term, $like_escaped_term ), ARRAY_A ); 

            if ( empty( $sites ) )
                        wp_die( -1 ); 

            foreach ( (array) $sites as $details ) {
                        $blogname = get_blog_option( $details['blog_id'], 'blogname' );
                    $return[] = array(
                            'label' => sprintf( '%1$s (%2$s)', $blogname, $details['domain'] . $details['path'] ),
                            'value' => $details['domain']
                        );
            } 

           $response = json_encode( $return );
            echo $response;
        exit();
}

Jquery autocomplete works without any errors but don’t get any data back.

need your help!

Thanks

Related posts

Leave a Reply