[英]ajax call based on the value of 2 input fields to populate jquery autocomplete
I have been stuck with this problem for a while. 我被这个问题困扰了一段时间。 I would like to pass 2 arguments (the value of 2 input fields of one form) in my ajax call to be used for a jquery autocomplete (the search is based on a mysql query using the values of input1 and input2).
我想在我的ajax调用中传递2个参数(一种形式的2个输入字段的值),以用于jquery自动完成(搜索基于使用输入1和输入2的值的mysql查询)。 I had a few suggestions but so far i have no luck: here my ajax call trying to pass the 2 arguments input1 and input2.
我有一些建议,但到目前为止我还没有运气:在这里,我的Ajax调用试图传递2个参数input1和input2。 there is no code error showing up but the autocomplete does not work.
没有出现代码错误,但自动完成功能不起作用。 it is working if i am using only one argument.
如果我仅使用一个参数,它将起作用。
function fillbox2(){
$('#input2').autocomplete({
source: function(request, response ){
var frmStr={
input1:$('#input1').val(),
input2:$('#input2').val()
requestTerm: request.term
};
$.ajax({
url: './cgi_temp3.cgi',
dataType: 'json',
data:{data: frmStr},
contentType: "application/json; charset=utf-8",
success: function (data) {
response ($.map( data.matches, function(item){
return {
value: item.info2,
}
}));
}
});
},
minLength: 2,
select: function(event, ui){
$("#prod_term").val(ui.item.value);
return false;
}
});
and here my cgi script that process the MYSQL query 这是我的处理MYSQL查询的CGI脚本
#!/usr/local/bin/python3
import cgi, json
import os
import mysql.connector
def main():
print("Content-Type: application/json\n\n")
form = cgi.FieldStorage()
term2 = form.getvalue('input2')
term1=form.getvalue('input1')
conn = mysql.connector.connect(user='***', password='***', host='localhost', database='***')
cursor = conn.cursor()
qry = """
SELECT name2, info2
FROM table2
join table1 ON
info2_id=information2_id
WHERE name2 LIKE %s AND info2_id=%s
"""
cursor.execute(qry, ('%' + term2 + '%',term1))
where could be the problem? 问题可能出在哪里?
At first glance I'd say it's possibly a timing issue. 乍一看,我想这可能是时间问题。 The source function isn't going to wait for your ajax call to complete, so you're essentially giving it a blank value.
源函数不会等待您的ajax调用完成,因此您实际上是给它一个空白值。 Try initiating the autocomplete inside the ajax success function.
尝试在ajax成功函数中启动自动完成功能。
function fillbox2(){
$.ajax({
...
success: function (data) {
...
$('#input2').autocomplete(...);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.