【发布时间】:2018-04-16 23:37:24
【问题描述】:
我必须在一个表单中输入 2 个输入字段和 2 个相应的提交 btns。当用户单击 btn1 时,表(table1)显示有关 input1 的信息(查询链接到处理 ajax 调用以显示 table1 的 js 文件 - mysql 查询由 cgi python 脚本运行 - 信息作为JSON 文件)。我为 input2 设置了相同的设置。这两个信息都是独立处理的。这部分正在工作。但现在我想使用 input1 作为过滤器来搜索 mySQL 查询中的 input2 信息。我最初认为我可以通过插入以下内容在我的 CGI 脚本中为 serach2 获取 input1 的值:
var field1=getvalue(#input1)
但这不起作用。所以当我处理search2的ajax调用时,我可能需要在我的js文件中引用input1。 这里是我的 search2 函数:
unction runSearch2( term) {
$('#table2').empty();
$('#table1').empty();
// here i used .serialize for input2
var frmStr = $('#form2 input').serialize();
$.ajax({
url: './cgi_temp2.cgi',
dataType: 'json',
data: frmStr,
success: function(data, textStatus, jqXHR) {
processJSON2(data);
},
error: function(jqXHR, textStatus, errorThrown){
alert("Failed to perform gene search! textStatus: (" + textStatus$
") and errorThrown: (" + errorThrown + ")");
}
});
}
这里是我的 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')
#trying here to get the value of input1 (information1_id)
#term1=getvalue('input1')
conn = mysql.connector.connect(user='user1', password='psd1', host='host1'
cursor = conn.cursor()
qry = """
SELECT name_input2, age_input2, city_input2
FROM info2
join info1 ON
info2_id=information1_id
WHERE name_input2 LIKE %s and info2_id=%s
"""
# adding another query condition based on the value of input1
cursor.execute(qry, ('%' + term + '%',term1))
results = { 'match_count': 0, 'matches': list() }
for (name_input2, age_input2, city_input2) in cursor:
results['matches'].append({'name_input2': name_input2, 'age_input': age_input2,'city_input2':city_input2})
results['match_count'] += 1
conn.close()
#send the value back to the JS file to be display
print(json.dumps(results))
如何引用 input1 以便在我的 mysql 查询中使用它来查询 input2?
【问题讨论】:
标签: python jquery json ajax cgi