Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI autocomplete: how to send post data?

From jQuery UI site (veiw source):

$( "#birds" ).autocomplete({
    source: "search.php",
    minLength: 2,
    select: function( event, ui ) {
        log( ui.item ?
            "Selected: " + ui.item.value + " aka " + ui.item.id :
            "Nothing selected, input was " + this.value );
    }
});

So as I see there is no options how to make ajax request with post data to the "search.php".

But I need to do that to send some filter from previous input field (current field: city, previous field: country).

How to do that?

Thanks!

like image 756
Vitalii Ponomar Avatar asked Nov 28 '11 18:11

Vitalii Ponomar


People also ask

How can create autocomplete textbox from database in jQuery?

Add web form right click on project add new item and choose web form. Add script and styles cdn link in head section. Design HTML by grad and drop textbox control in web form. Complete html code of web form.

How does autocomplete work in jQuery?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

What is jQuery ui autocomplete?

Advertisements. Auto completion is a mechanism frequently used in modern websites to provide the user with a list of suggestions for the beginning of the word, which he/she has typed in a text box. The user can then select an item from the list, which will be displayed in the input field.

What is UI autocomplete?

ui-autocomplete : The menu used to display matches to the user. ui-autocomplete-input : The input element that the autocomplete widget was instantiated with.


2 Answers

Try changing the source to be a method which uses $.post:

$("#birds").autocomplete({
  source: function (request, response) {
    $.post("search.php", request, response);
  },
  ...
like image 120
matt Avatar answered Sep 21 '22 17:09

matt


$( "#birds" ).autocomplete({ 
source: function (request, response) {
    $.ajax({
  type: "POST",
  url:"search.php",
  data: request,
  success: response,
  dataType: 'json'
});
  }
}, {minLength: 3 });

//-------------------------
//search.php - example with request from DB

//


 $link = mysql_connect($mysql_server, $mysql_login, $mysql_password)
        or die("Could not connect: " . mysql_error());
     mysql_select_db($mysql_database) or die("Could not select database");
     mysql_set_charset('utf8'); 

$req = "SELECT mydata FROM $mysql_table WHERE mydata LIKE '".$_REQUEST['term']."%' ORDER BY mydata ASC";
$query = mysql_query($req);

while($row = mysql_fetch_array($query))
{
    $results[] = array('label' => $row['mydata']);
}


echo json_encode($results);
?>
like image 44
Yuriy Bondarenko Avatar answered Sep 19 '22 17:09

Yuriy Bondarenko