Riya Suzanne
Riya Suzanne

Reputation: 25

Pass a parameter in Autocomplete AJAX request

I am working on an autocomplete option to be integrated. Autocomplete is working fine. But when i add another parameter variable the autocomplete is not working, guess kinda syntax problem. Here in the below script attached, i need to pass variable countrycode to fetch_customers.php

$(document).ready(function($) {
  $("#customers").autocomplete({
    var countrycode = '<?php echo $agencyid; ?>';
    data: {
      countrycode: countrycode
    },
    source: "fetch_customers.php",
    minLength: 2,
    select: function(event, ui) {
      var url = ui.item.id;
      if (url != '#') {
        location.href = '/view-customer/' + url;
      }
    },
    // optional (if other layers overlap autocomplete list)
    open: function(event, ui) {
      $(".ui-autocomplete").css("z-index", 1000);
    }
  });
});

Upvotes: 0

Views: 609

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337733

Your syntax is not valid. You would need to define countrycode outside of the object you provide to autocomplete().

With that said, that's not how you pass data in a jQueryUI Autocomplete request. You instead need to pass the value in the querystring of the URL you call:

$(document).ready(function($) {
  $("#customers").autocomplete({
    source: "fetch_customers.php?countrycode=<?php echo $agencyid; ?>",
    minLength: 2,
    select: function(event, ui) {
      var url = ui.item.id;
      if (url != '#') {
        location.href = '/view-customer/' + url;
      }
    },
    open: function(event, ui) {
      $(".ui-autocomplete").css("z-index", 1000);
    }
  });
});

Upvotes: 1

Related Questions