frazman
frazman

Reputation: 33243

passing arguments to python function by javascript

Since mostly a backend guy, I am not sure how can I achieve the following since it requires some interaction with the browser.

So, I have a the following things so far.

A communication protocol where server is in python and client is in javascript code. Ultimately, I want my data to reach to that javascript code.

Now, this data is being captured from browser.

As a practice.. what I am trying to do is.. have two radio buttons on my browser and a submit button

  *radio A
  *radio B
  * Submit

Now, when the user presses submit, I somehow want to create a query "user submitted: A (or B)" and this query i am able to capture on python script.

I am at lost on how to do this.

My guess is that "submit" invokes a python script. But what if my python server is always on .. how do i parse that response from the click of browser to this python server?

Upvotes: 1

Views: 156

Answers (1)

nneonneo
nneonneo

Reputation: 179442

This is the way it usually works:

  1. Client (browser) visits webpage and initiates request to server
  2. Server (in your case, Python) handles request and writes HTML response, including the radio-button form
  3. Client fills out form and hits Submit, triggering another request to the server
  4. Server handles the second request and writes another response (e.g. "Purchase successful", "message posted", etc.).

Note that the second request is a brand-new request. You may want some way of linking the first request to the second one unless the second request is anonymous. Some frameworks will do that for you, but if you are making the server from the ground up you'll want some kind of session mechanism to keep track of state.

To get the client to make the second request, the simplest is to add appropriate action and method attributes to the form element in your HTML. action specifies the URL to access for the form request, and method is either GET or POST. (More advanced usage, e.g. on this site, typically uses AJAX to make the submissions instead).

Upvotes: 2

Related Questions