Christos Papoulas
Christos Papoulas

Reputation: 2578

Autocomplete multiple field

I have a mysql db and I want to autocomplete a form from values of different tables for each input tag.

Let's say I have the following htlm:

<form>
a: <input type="text" name="a" id="a"><br>
b: <input type="text" name="b" id="b"><br>
c: <input type="text" name="c" id="c">
</form>

And this piece of Jquery code that works fine:

jQuery(document).ready(function($) {
  $('#a').autocomplete({
    source: 'http://localhost/elgg/application_description/search_app.php',
    minLength: 2
  });
});

The problem is how can I make the autocomplete works for id b and c and the php file knows about what field autocompletes each time?

I have thing that I can create another 2 files of php that responses with the auto complete values but that it seems not efficient. The other solution is how can pass parameters to search_app.php so it knows about where to search?

Upvotes: 0

Views: 1328

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try this,

jQuery(document).ready(function($) {
   $('#a, #b, #c').autocomplete({
      source: function(req,res){
        $.ajax({
          url:'http://localhost/elgg/application_description/search_app.php',
          dataType: 'json',
          data: { 
            term  : $(this).val(),
            id: this.id,
          },
          success: function(data){
            alert(data);
          }
      },
      minLength: 2
   });
});

Upvotes: 2

Related Questions