【问题标题】:Jquery ajax mysql search based on 2 input fields基于2个输入字段的jQuery ajax mysql搜索
【发布时间】: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


    【解决方案1】:

    如果您对输入进行序列化,它将只返回该输入。您应该序列化整个表单,这样您就可以在后端获得这两个值。 getvalue('input1') 不起作用,因为您只是没有发送该参数。

    【讨论】:

    • 如何使用 2 个输入字段一次序列化整个表单?
    • 如果您的表单form2 包含这两个字段,则它只是var frmStr = $("#form2").serialize();。如果没有,只需删除serialize() 并自己构建参数var frmStr={input1:$("#input1").val(),input2:$("#input2").val()};(将#input1#input2 替换为正确的选择器以到达每个字段)。
    • 非常感谢......它现在正在工作......现在我需要解决 input2 上的 jquery 自动完成功能。但这可能是另一个问题!
    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多