简体   繁体   中英

Sending additional parameters from jQuery auto complete to a php page

Hi guys basically I want to know is there a way to send additional parameters from jQuery autocomplete to a php page that then queries a database and sends the result back. I know about sending the term that has been typed into the input box but would like to know if you could send for example the selected option from a select box.

Here is my code:

<script src="/scripts/jquery-ui-min"></script>
<script>
 $('#tags').autocomplete({
 search: function(event, ui) {
     $('.test ul').empty();
 },
 source: '/autoCompleteKnowledgeBase.php'
 }).data('autocomplete')._renderItem = function(ul, item) {

 return $('<li/>')
 .data('item.autocomplete', item)
.append(item.value)
.appendTo($('.test ul'));
};
</script>
<form method="POST" action="askAQuestion.php">
 <p>
 Search for:
 <input type="text" id="tags" name="searchFor">
 <input type="submit" id="tags" value="Find ...">
 </p>
 </form>
 <div class="test">Output goes here:<br/><ul></ul></div>
 </div>

The autoCompleteKnowledgeBase.php takes the term and does a database query on it and then sends the result back in JSON. Looking for a way to pass additional parameters such as selected option.

Any Ideas?

Thanks

You would be able to achieve that like this:

var additionalDataToServer;

$('#tags').autocomplete({
 search: function(event, ui) {
     $('.test ul').empty();
 },
 source: function( request, response ) {
   //set any additional data to the request here... for example

   additionalDataToServer = $('#someInput').val();
   request.additionalDataToServer = additionalDataToServer;


   $.getJSON( '/autoCompleteKnowledgeBase.php', request, function( data, status, xhr ) {
      response( data );
   });
  }
 }).data('autocomplete')._renderItem = function(ul, item) {

 return $('<li/>')
 .data('item.autocomplete', item)
.append(item.value)
.appendTo($('.test ul'));
};

In server, request.term is the value typed by user & request.additionalDataToServer is the one which you added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM