Link to home
Start Free TrialLog in
Avatar of sara_bellum
sara_bellumFlag for United States of America

asked on

Launch python function as html onClick event

I have a python-generated html form that lists values retrieved from a mysql database. The form has two submit buttons that work:  database records are initially sorted by name or by record number depending on the submit button selected.  

Table row:     Last Name      First Name     Submitted on:                  View Record
<TR>              Brando           Marlon            timestamp                              1

The last table cell of each row source:
<input type="hidden" name="id" value="1"><a onClick="identify_record(1)" href="">1</a>

I am looking for a way to call the function as an onClick event and to pass the student ID argument so that when the user clicks on the record number in the page, the record number is passed to the function.

The python function is in the module called by the html form:

def identify_record(student_id):
   
    print student_id

The print statement fails so I must be missing something. I have tried a number of ways to do this and nothing seems to work so far.  Let me know your thoughts thanks.
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sara_bellum

ASKER

Thanks! I thought this might be the case and tried a number of javascript options, all of which failed for me so I'm moving on. I'm changing the html to:

href="http://localhost/script_name.py?student_id=%s" % student_id

The destination script must parse the url to extract the student_id number. So far I can parse a static url. I don't know how to capture a url from the href to save the url/query as a variable.  Any ideas on this approach and how to do it are welcome, thanks.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks routinet, I'll try that.  I did find a way to import the paramater as a form value into the destination script - in this way form.getvalue('student_id') gives me the parameter I need for the sql query.  The output of my query is looking dicey at the moment, will work on that and post what I find. I'll need to work the output in XML rather than in HTML, for starters.
In conclusion, the student_id is appended as an argument to the destination url. The destination script then reads the argument that is passed to it as a form value.  

This 2d script executes an sql query to select all data for the student where the student_id argument/value matches the (unique) corresponding value in the database. The field names and values for the selected student are then printed to the html page.    

This approach limits output to one record at a time, but that wasn't the subject of my question.  This is, after all, the crawl stage of a crawl, walk, run sequence :-)  It's late, I'll award points tomorrow.
Another slightly more complex, but not that different approach is, that the javascript performs an asynchronous post request and that the student id (student id's) are posted in a machine readable format (XML or in my opinion preferably JSON), that the script then returns machine readable result (XML or json) and that your javascript then renders the result within your existing HTML page.

It all depends what exactly you try to achieve.

Do you want to display a new HTML document or do you want to 'integrate' the result in an existing HTML page.
In this case I was using python to print mysql values to an html page that is loaded when the script is called by the browser. The result is integrated, somewhat:  I write to a "body.html" page, and copy both header.html and body.html to the destination page that is loaded.  

This approach works when displaying mysql table values.  But when inserting form values into mysql via python cgi, apache modsecurity warns of sql injection attacks when the user is prompted to review and confirm form inputs.  

I was importing a cookie for session control but the script only worked on my lab PC.  Submitting form values into mysql from any other PC is blocked by apache with access denied due to modsecurity errors. Back to the drawing board.
I researched json and I see how you can use it to dump a mysql database to a flat file, so I'm assuming I could display that file in an html page which is good to know. But it doesn't address the problem I have at the moment so I'll close this.