Home Jquery ajax mysql search based on 2 input fields
Reply: 0

Jquery ajax mysql search based on 2 input fields

user7600
1#
user7600 Published in September 19, 2018, 11:01 am

I have to 2 input fields with 2 corresponding submit btns in a form. when the user clicks btn1, a table(table1) displays info about input1 (the query is linked to a js file which process ajax call to display table1 - the mysql query is ran by a cgi python script -, the info is passed as a JSON file). and I have the same set up for input2. both info are processed independently. and this part is working. BUT now I would like to use input1 as a filter for the search of input2 information in mySQL query. I initially thought that That I could get the value of input1 in my CGI script for serach2 by inserting:

var field1=getvalue(#input1)

but this not working. so I probably need to refer to input1 in my js file when I process the ajax call for search2. here my search2 function:

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 + ")");                
    }                                                                     
});                                                                                                                                 
}        

and here my CGI file:

#!/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))           

how do I refer to input1 so I can use it in my mysql query for input2?

share|improve this question

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.404613 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO