javascript - WordPress autocomplete search: how to connect query file of custom table? -


the aim retrieve data custom table , display in auto-complete form on web-site, running wordpress. here table.

firm_id       firm_name    1        helsinki genetics    2           23andme 

i use jquery-autocomplete library , advises this topic. how fill functions.php of wordpress theme. guess, somewhere here mistake..

// add jquery-autocomplete library  function my_library_method() { // register script location, dependencies , version    wp_register_script('library_script', get_template_directory_uri() . '/js/jquery.autocomplete.js');  // enqueue script    wp_enqueue_script('library_script');   } add_action('wp_enqueue_scripts', 'my_library_method');      // add javascript file autocomplete  function my_scripts_method() {   wp_register_script('custom_script', get_template_directory_uri() . '/js/js_auto.js');   wp_enqueue_script('custom_script');   } add_action('wp_enqueue_scripts', 'my_scripts_method'); 

this code input box.

<input type="text" name="mybox" id="mybox" /> 

this js_auto.js file, makes auto-complete magic.

jquery(document).ready(function($) {    $('#mybox').autocomplete({  // add way file database query         serviceurl: 'tags.php',  // happens when user chooses autocomlete suggestion         onselect: function (suggestion) {         alert('you selected: ' + suggestion.value + ', ' + suggestion.data);     }   });  }); 

finally tags.php file, query database.

    $query = isset($_get['query']) ? $_get['query'] : false;      global $wpdb;      // escape values passed db avoid sql-injection      $depts = $wpdb->get_results( "select * wp_firms firm_name '".$query."%' order firm_name asc" );      $suggestions = array();     $data = array();     foreach($depts $row) {         $suggestions[] = $row->firm_name;         $data[] = $row->firm_id;     }     $response = array(         'query' => $query,         'value' => $suggestions,         'data' => $data,     );     echo json_encode ($response); 

i tested autocomplete manually filled array in js_auto.jsfile without tags.php, , worked perfectly. want data array database.

any ideas how connect tags.php (database query) js_auto.js (autocomplete script)?

sorry bit long explanation tried describe detailed possible. i'm trying solve second weekend appreciated.

it might need change serviceurl full path:

serviceurl: 'http://yourdomain.com/path/tags.php', 

depending on you're hosting tags.php file. use

var template_uri = <?php get_template_directory_uri(); ?>; serviceurl: template_uri + '/tags.php', 

try using developers tools in chrome example, , show there js error is.

and replace:

foreach($depts $row) {     $suggestions[] = $row->firm_name;     $data[] = $row->firm_id; } $response = array(     'query' => $query,     'value' => $suggestions,     'data' => $data, ); 

with:

foreach($depts $row) {     $data['value'] = $row->firm_name;     $data['data'] = $row->firm_id; } $response = array(     'suggestions' => $data; ); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -