Ajax Search Form

jQuery(document).ready(function() {jQuery
jQuery(‘#schoolword’).on(‘input’, function() {
var searchKeyword = jQuery(this).val();
var searchType = jQuery(‘#searchby’).val();
if (searchKeyword.length >= 3) {
jQuery.post(‘/includes/schoolsearch-ajax.php’, { keywords: searchKeyword,searchtype: searchType }, function(data) {
jQuery(‘ul#schoollist’).empty()
jQuery.each(data, function() {jQuery(‘ul#schoollist’).append(‘<li>//<a href=”//’+this.link+'”>’ + this.t//itle + ‘</a></li//>’);

 

‘);
});
}, ‘json’);
}
});
});

Search Form

” value=”” name=”s” />

    ajax-search.php

    <?php
    require_once(‘../../../wp-load.php’);
    global $wpdb;
    if ( !function_exists( ‘is_woocommerce_active’ ) ) require_once ‘../../plugins/woocommerce/woocommerce.php’;
    $arr = array();

    if (!empty($_POST[‘keywords’])) {
    $keywords = $_POST[‘keywords’];
    $searchtype = $_POST[‘searchtype’];
    if($searchtype == ‘product’){
    $sql = “SELECT ID, post_title FROM wp_posts WHERE post_title LIKE ‘%”.$keywords.”%’ LIMIT 10″;
    $posts = $wpdb->get_results($sql);
    if(count($posts)>0){
    foreach ($posts as $post)
    {
    $link= get_permalink($post->ID);
    $arr[] = array(‘link’ => $link, ‘title’ => $post->post_title);
    }
    }
    }
    else{
    $sql1 = “SELECT slug, name FROM wp_terms WHERE name LIKE ‘%”.$keywords.”%’ LIMIT 10″;
    $categories = $wpdb->get_results($sql1);
    if(count($categories)>0){
    foreach ($categories as $woo_cat)
    {
    $link= get_term_link( $woo_cat->slug, ‘product_cat’ );
    $arr[] = array(‘link’ => $link, ‘title’ => $woo_cat->name);
    }
    }
    }
    if(count($arr)==0){
    $arr[] = array(‘link’ => ‘#’, ‘title’ => ‘Nothing Found’);
    }
    }
    echo json_encode($arr);
    ?>

    Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    Create a free website or blog at WordPress.com.

    Up ↑

    %d bloggers like this: