简体   繁体   English

将其他参数从jQuery自动完成发送到php页面

[英]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. 大家好,基本上我想知道是否有一种方法可以将其他参数从jQuery自动完成发送到php页面,然后查询数据库并将结果发送回去。 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. autoCompleteKnowledgeBase.php使用该术语并对其进行数据库查询,然后将结果发送回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. 在服务器, request.term是由用户输入的值request.additionalDataToServer为你添加了一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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