Ajax result get’s 0 appended
-
Hello,
I have a really annoying problem here.
Whenever I do an ajax call, the result gets 0 or -1 appended.This is for a “search suggestion” tool.
I’m using jquery’s $.post function.
Javascript:$.post( ajaxurl, { action:'finditem', term:$('#suggest-search').val() }, function(reply) { $('.suggestr').html(reply); $('.suggestr').show(); } );PHP:
function ajaxSearchSuggest() { global $wpdb; $term = $_POST['term']; $sql = $wpdb -> prepare("SELECT <code>ID</code> FROM <code>wp_posts</code> WHERE <code>post_title</code> LIKE %s AND <code>post_status</code> = 'publish' AND <code>post_type</code> = 'post'", '%' . $term . '%'); $results = $wpdb -> get_results($sql); foreach($results as $result) { $post = get_post($result -> ID); $categories = wp_get_post_categories($post -> ID); if(in_array(4, $categories)) { echo '<a href="' . get_permalink($result -> ID) . '">' . $post -> post_title . '</a>'; } } } add_action('wp_ajax_finditem', 'ajaxSearchSuggest'); add_action('wp_ajax_nopriv_finditem', 'ajaxSearchSuggest');I get the correct results, just with “0” appended. I’ve previously used string functions to exclude it, but it’s a crappy solution, and I really want to figure out why it’s doing this.
If anyone can give me a hint I’d be really gratefull.
Thanks!
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Ajax result get’s 0 appended’ is closed to new replies.