1

I am new to Flask,HTML.Kindly please help me. I am unable to get the parameter values from HTML form(index1.html) My HTML code is

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@@</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>
    <script type=text/javascript> $(function() { $("#mybutton").click(function (event) { $.getJSON('/add_text', { },
    function(data) { }); return false; }); }); </script> 
</head>
<body>
    <form  action="/add_text" method="POST">
         <h1 style="color:green;">CALCUATOR</h1><br><br>
         <label for="textv">First_No:</label><br>
         <input type="number" name="textv" id="textv" value=""><br>
         <label for="text">Second_No:</label><br>
         <p></p><input type="number" name="text" id="text" ><br><br></p>
         Result:<span id="result"></span><br><br><br><br><br><br>
         <input type = "submit" class="btn btn-primary btn-lg" id = "mybutton" value = "Addition" />
    </form> 
</body>
</html>

My Flask tempalte.py file is

from flask import Flask, jsonify, render_template, request,redirect, url_for
app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index1.html')

@app.route('/add_text',methods=["POST", "GET"])
#print("hi")
def Addtext():
    print("hi")
    print(request.method == "POST")
    if request.method == "POST":
        print("hello")   
        text_value = request.form.get('textv')
        text_value1 = request.form.get("text")
        #add=request.form["addition"]
        print("hi")
        print(text_value)
        print("hello")
        #text_value=int(text_value)
        #text_value1=int(text_value1)
        #print(text_value+text_value1)
        print('In SomeFunction hello $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$4')
    return 'NOTHONG'



if __name__ == '__main__':
   app.run(debug=True)   

Please kindly help me to fix the issue for getting the data from html form

  • Does this answer your question? [Get the data received in a Flask request](https://stackoverflow.com/questions/10434599/get-the-data-received-in-a-flask-request) – polo-language Jan 03 '22 at 20:51

1 Answers1

0

It looks like you have currently only one event firing on the button click which is a GET request. Now I assume you want to to do the following:

  • Input numbers
  • click button send numbers to back end (POST)
  • After button is clicked and calculation is complete get the result (GET)

You can implement events for both POST and GET behavior on the same click. See the answer to this question for an example. There are many more.

Maarten
  • 492
  • 7
  • 14