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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Please correct me if my assumptions are wrong:

I assume, you use a standard web browser and that you use a web server running python in one of the following ways
-  python via CGI
-  mod_python of apache
- python via wsgi
- python via some other frame work like for example Django

If my above assumptions are right, then:

You cannot directly call a Python function from Javascript.

The onClick event handling is done by javascript on the web browser on the remote PC.
Your python code resides on the server and therefore its functions cannot be called directly.

What you can do however is:
- write a javascript function, that is being colled whenever somebody clicks on the identifier, The javascript could then extract the value out of the identifier field and initiate an asynchronous request to the server (a specific url), which could then run your python code. if your python code returns some values these values could be retrieved by the java script.

The best approach really depends on what exactly you like to do.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sara_bellumAuthor Commented:
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/" % 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.
Steve BinkCommented:
As gelonida explained, javascript is a key component to this strategy.  How I would work it:

Craft the input as:
<input type="button" name="identify" id="id_1" value="ID" />

Open in new window

Using jQuery (or similar framework), intercept the onclick event.  The new onclick should execute an AJAX call back to your server.  The POST data should include at least the information from the clicked button (the id attribute), if not the entire form.  The page fielding that request would dump into your identify_record() function.

Also, change identify_record() to accept a request object.  That way, you are not tied in to a specific interface should future changes be necessary, and the work necessary to isolate the values you need can be kept local to the function.  If you're using WSGI, that means identify_record() can become a direct WSGI call.  Other platforms would still benefit from the simpler calling mechanism.

Finally, remember to have identify_record() return an appropriate value.  For an AJAX call, appropriate is whatever the client-side routines are expecting.  This could be XML, HTML, or even plain-text.  Try to make your return values consistent across all parts of your application.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

sara_bellumAuthor Commented:
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.
sara_bellumAuthor Commented:
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.
sara_bellumAuthor Commented:
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.
sara_bellumAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.