-1

I have a button which when clicked just need to execute a javascript function. The javascript function returns a string which I intend to capture in the flask backend. Currently the javascript function runs on onclick.

The code:

<form id='details'method="post">
     <input class="myButton" id="mybutton" type="submit" value="Save" onclick="getdata()">
     </input>
</form>

The flask code:

app.add_url_rule('/entry1', endpoint='entry1', view_func=new_data, methods=['GET', 'POST'] )

def new_data():
    if request.method == 'POST':
        details = request.form['details']
        print(details)

In flask, when I am giving request.forms['details'], i don't get anything. I expect to get returned data from getdata. I am pretty sure, theres some issue in html side. Please advice.

Got a lot of comments regarding the same, but it doesn't fix...just wonder if there is any other way to handling it, I just need text collected on button click to be transferred to python backend, it seems pretty simple, but just cant get this done :( Thanks

arnab dasgupta
  • 219
  • 2
  • 3
  • 9

2 Answers2

0

#EDIT 2 check this answer Here.

I report here the most of it as referance:

  • [request.args][2]: the key/value pairs in the URL query string
  • [request.form][3]: the key/value pairs in the body, from a HTML post form, or JavaScript request that isn't JSON encoded
  • [request.files][4]: the files in the body, which Flask keeps separate from form. HTML forms must use enctype=multipart/form-data or files will not be uploaded.
  • [request.values][5]: combined args and form, preferring args if keys overlap
  • [request.json][6]: parsed JSON data. The request must have the application/json content type, or use [request.get_json(force=True)][7] to ignore the content type.

All of these are [MultiDict][8] instances (except for json). You can access values using:

  • request.form['name']: use indexing if you know the key exists
  • request.form.get('name'): use get if the key might not exist
  • request.form.getlist('name'): use getlist if the key is sent multiple times and you want a list of values. get only returns the first value.

First it would be helpful if you also add the flask code. That's being said, add this to the input tag:

<form id='details'method="post">
     <input class="myButton" name="details" id="mybutton" type="submit" value="Save" 
     onclick="getdata()">
 </input>
</form>

So basically you had a 'name=""' field to it.

Then in the python file, use this:

request.form['details']

Even better if you store the value in a variable (obviously that's depend you, is just a suggestion)

request_detail = request.form['details']

Also in the form tag you may want to add a 'action=""' field. This depends on how route Decorator in the flask.

Example if you have this:

@app.route('/details)
def details():
    pass

Then your form should have this:

<form id='details'method="post" action="/details">

or

<form id='details'method="post" action="details">

EDIT

Try to use a normal route decorator to see if it fixes:

@app.route('/entry1', methods=['GET', 'POST'])
def new_data():
    if request.method == 'POST':
        details = request.form['details']
        print(details)
    return render_template('filename.html')

If you still want to use the add_url_rule try this:

app.add_url_rule('/', 'entry1', view_func=new_data, methods=['GET', 'POST'] )

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
0

You are getting nothing in flask request.form['details'], because the data was sent using javascript. You should try:

request.data  # if data was sent as plain text

or

request.get_json() # if data was sent as json
Faisal Khan
  • 548
  • 3
  • 16
  • Oops, nope, I dont get anything if i say details=request.data, i just get b'' as details value...am badly stuck in seemingly an innoocous problem :( – arnab dasgupta Aug 08 '20 at 01:35