views:

83

answers:

3

Hi,

I'm using the JQuery Autocomplete in one of my forms.

The basic form selects products from my database. This works great, but I'd like to further develop so that only products shipped from a certain zipcode are returned. I've got the backend script figured out. I just need to work out the best way to pass the zipcode to this script.

This is how my form looks.

<form>

<select id="zipcode">

<option value="2000">2000</option>
<option value="3000">3000</option>
<option value="4000">4000</option>

</select>

<input type="text" id="product"/>

<input type="submit"/>

</form>

And here is the JQuery code:

$("#product").autocomplete
({
     source:"product_auto_complete.php?postcode=" + $('#zipcode').val() +"&",
     minLength: 2,
     select: function(event, ui){

                             //action

                                 }
});

This code works to an extent. But only returns the first zipcode value regardless of which value is actually selected. I guess what's happening is that the source URL is primed on page load rather than when the select menu is changed. Is there a way around this? Or is there a better way overall to achieve the result I'm after?

A: 

I believe you are correct in thinking your call to $("#product").autocomplete is firing on page load. Perhaps you can assign an onchange() handler to the select menu:

$("#zipcode").change(resetAutocomplete);

and have it invalidate the #product autocomplete() call and create a new one.

function resetAutocomplete() {
    $("#product").autocomplete("destroy");
    $("#product").autocomplete({
         source:"product_auto_complete.php?postcode=" + $('#zipcode').val(),
         minLength: 2,
         select: function(event, ui){... }
    });
}

You may want your resetAutocomplete() call to be a little smarter -- like checking if the zip code actually differs from the last value -- to save a few server calls.

Paul Schreiber
Thanks! This works really well.
matt
great! please accept the answer.
Paul Schreiber
Destroying the autocomplete widget and recreating it is *tremendously* wasteful, you should use the `source` argument as it was intended...
Nick Craver
A: 

You need to use a different approach for the source call, like this:

$("#product").autocomplete({
  source: function(request, response) {
    $.getJSON("product_auto_complete.php", { postcode: $('#zipcode').val() }, 
              response);
  },
  minLength: 2,
  select: function(event, ui){
    //action
  }
});

This format lets you pass whatever the value is when it's run, as opposed to when it's bound.

Nick Craver
matt
@matt - You'll just be using `$_GET['postcode']` with the above code...if you want term on there, add it in by using this for the data argument: `{ term: request.term, postcode: $('#zipcode').val() }`
Nick Craver
@Nick Still having trouble getting it working. I've made the change, but the autocomplete fails to materialise. Jquery is very new to me, so I'm not sure how to debug, but it seems to stop all code from my javascript file from working. I'm not sure what might be out of place, but the "request.term" is new to me.Just to clarify, this is the "term" that autocomplete creates as you type, right?
matt
@matt - Correct that's where it comes from...the error was a missing comma after the `source` option in the above code [see here](http://stackoverflow.com/posts/3694296/revisions), sorry! The console is the best place for debugging all JS errors, either Firebug or Chrome include excellent consoles for this.
Nick Craver
Works great! Much leaner code. Thank you.
matt
@Nick - Hi Nick, Hope you don't mind me asking but; Are you able to explain, or point me in the right direction to understanding, how the function(request, response) changes it from being when it's bound to being when it's run? - Also: is the response some type of output parameter, and where does the request parameter come from/get used? (I'm pretty new to this stuff atm). Cheers.
James
@King - `response` is a function passed in when it's called, it's a callback function that itself takes a single parameter, the response text or data...that function sticks the data in the autocomplete widget. `request` is also provided, it's an object with a `term` property which is what's currently types in the box, it's passed in when the widget calls this method.
Nick Craver
@Nick - Thanks very much mate, really appreciate the explanation!
James
A: 

Thank you very much!!!

asm