0

I have a problem with tow jquery function, I have a autocomplete search box on form and tow function in script part. I want a function fetch a list and other one autocomplete text box but can`t call fetch function at first. It run after autocomplete function all the time.Can every one help me?

1- fetch list function is:

$(function ()   
{
    $.ajax({
            url: 'fetch_user_list_from_user_table.php',
            data: "{}",
            dataType: 'text',
            success: function(data)
            {   
                user_list=data;
                 alert("list is: " + user_list);
            },
            error: function()
            {
                $('#output').html("Error");
            }
            });
});

2- autocomplete function

$(function ()
{
    var availableTags;
    availableTags =user_list;
    alert("tags are"+availableTags);
    $( "#tags" ).autocomplete(
    {
        source: availableTags
    });
});

3- html part

<input id="tags" type="text" name="message_receiver_title"> </input>
Gaurav Aggarwal
  • 9,809
  • 6
  • 36
  • 74
  • use response(data) instead of return data. It shoud work. http://stackoverflow.com/questions/21385892/how-to-use-source-function-and-ajax-in-jquery-ui-autocomplete – daremachine Jun 10 '16 at 13:07

2 Answers2

0

call the ajax function as a callback function in autocomplete.

$(function() {
      var availableTags;
      availableTags = user_list;
      alert("tags are" + availableTags);
      $("#tags").autocomplete({
        source: function(request, response) {
          $.ajax({
            url: 'fetch_user_list_from_user_table.php',
            data: "{}",
            dataType: 'text',
            success: function(data) {
              user_list = data;
              alert("list is: " + user_list);
            },
            error: function() {
              $('#output').html("Error");
            }
          })
        }
      });
Gaurav Aggarwal
  • 9,809
  • 6
  • 36
  • 74
0

If you really need to run 2 separated ajax calls then you can use $.when function which provide what you need. But @GauravAggarwal solution is better approach.

daremachine
  • 2,678
  • 2
  • 23
  • 34