bStaq
bStaq

Reputation: 127

jQuery Autocomplete Error in Wordpress

I am trying to implement an auto complete feature in wordpress but am a getting the following error:

Fatal error: Call to undefined method stdClass::get_results() in >'...get_airports.php' on line 9

The following is my code:

html: <input type="text" name="airports" id="airports" />

php: global $wpdb;

$q = strtolower($_GET["q"]);
if (!$q) return;

$wpdb->iata_airport_codes = $wpdb->prefix . "iata_airport_codes"; 
$airport_list_db = $wpdb->
get_results("SELECT * FROM `wp_iata_airport_codes` WHERE `airport` LIKE '%$q%' LIMIT   0,15");

foreach($airport_list_db as $airports){

 echo $airports->airport . "\n";

}

js: jQuery(document).ready(function() {
$("#airports").autocomplete("get_airports.php", {
    width: 230,
    matchContains: true,
    selectFirst: false
});
 });

any help will be much appreciated thanx.

Upvotes: 1

Views: 325

Answers (2)

realloc
realloc

Reputation: 177

You should insert

require_once('wp-blog-header.php');

before your code when you want to use $wpdb in a file which is not a part of a WordPress plugin and is not present in the functions.php of your theme.

BTW It would be much better if you would create a custom handler for your AJAX request.

Upvotes: 1

bStaq
bStaq

Reputation: 127

solved it!!! I connected to db directly using mysql_connect then used mysql_fetch_array($result, MYSQL_ASSOC) to loop though the result and display it. That worked but still dont know where the error was coming from probably because I put the 'get_airports.php' outside of wordpress in the root dir of the site. Thanx for all the help.

Upvotes: 0

Related Questions