• Resolved cimatadev

    (@cimatadev)


    Hi,

    I have some trouble to use autocomplete with jquery.

    I think that the function works because I can find this in my code :

    <ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content" id="ui-id-1" tabindex="0" style="display: none;"></ul>

    But the results do not display because I have this error :
    Fatal error: Call to a member function prepare() on null in /home/clients/717b0e9cd144e922df74206711a41135/web/cimaya/wp-content/themes/cimaya/autocomplete.php on line 19

    /autocomplete.php

    function get_liste_resultat_reference( $q = '' ) {
      global $wpdb;
      $sql = $wpdb->prepare(...);    <- line 19
    }

    /inc/main.js

    $('#rech-nav').autocomplete({
      source: $.get('/cimaya/wp-content/themes/cimaya/autocomplete.php'),
      appendTo: "#rech-nav",
      autoFocus: true,
      minLength: 2,
    });

    I think that the problem is wpdb.

    Can someone help me ? I am becoming crazy !

    Thanks !!

    PS : Sorry for my english

Viewing 12 replies - 1 through 12 (of 12 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    It appears as though “$wpdb” is empty.

    Thread Starter cimatadev

    (@cimatadev)

    “$wpdb” is supposed to work every where, no ?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Not if you’re targeting a file in your theme directly without using WordPress.

    Thread Starter cimatadev

    (@cimatadev)

    I am sorry, I did not understand

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You put your file inside the theme directory, but that does not mean you are using WordPress. You are effectively calling the file as if it were outside of WordPress.

    Thread Starter cimatadev

    (@cimatadev)

    Now I get it, thanks !

    How can I fix it ? Because I can not move my file out the theme directory and “$wpdb” works in every other files so I do not know how to make it work in this one.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    works in every other files so I do not know how to make it work in this one.

    Are you sure? Try targeting one of these files (that you think works) and see if the “wpdb” variable is defined:

    $('#rech-nav').autocomplete({
      source: $.get('/cimaya/wp-content/themes/cimaya/[the-other-file].php'),
      appendTo: "#rech-nav",
      autoFocus: true,
      minLength: 2,
    });

    Thread Starter cimatadev

    (@cimatadev)

    $('#rech-nav').autocomplete({
      source: $.get('/cimaya/wp-content/themes/cimaya/thematique.php'),
      appendTo: "#rech-nav",
      autoFocus: true,
      minLength: 2,
    });

    Fatal error: Call to a member function get_row() on null in /home/clients/717b0e9cd144e922df74206711a41135/web/cimaya/wp-content/themes/cimaya/thematique.php on line 21

    That is weird because this other file actually works on my site but not when I am targeting it in autocomplete

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I don’t think you understood what I meant by “You are effectively calling the file as if it were outside of WordPress”. Maybe I didn’t word it right.

    You are not using WordPress, so $wpdb will never exist. You think you are using WordPress because you put the file in the theme directory, but you are not using WordPress.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Thread Starter cimatadev

    (@cimatadev)

    You are not using WordPress, so $wpdb will never exist.

    How can I use WordPress, so $wpdb will exist ?

    Thread Starter cimatadev

    (@cimatadev)

    I added :

    require_once('../../../wp-load.php' );

    in my PHP script and it worked !

    Thanks for your help !

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Jquery Autocomplete Source’ is closed to new replies.