0

Im coding the following form in HTML:

<form action="#" method="post"> <input type="email" name="email"></input> <input type="email" name="email_conf"></input> <input type="submit" value = "Entrar"></input> </form> 

As Im using Flask, I would retrive the form data using request.form('email') and request.form('email_conf') and compare the content returned by the function.

But I would like to know if I could use Python in the .html file to compare the inputs by doing something like

<form action="#" method="post"> <input type="email" name="email"></input> <input type="email" name="email_conf"></input> {% if email == email_conf %} <input type="submit" value = "Entrar"></input> {% endif %} </form> 

Thanks for your help!

7
  • Basically you want to make the comparison Before the User hits the submit buttom or Before? Commented Jan 3, 2021 at 9:38
  • Thats true, my mistake. The data is going to be be stored after the submit buttom is clicked. Can I make the comparison after the click? Thanks for your response! Commented Jan 3, 2021 at 9:43
  • 3
    It would be best to just use javascript to make the comparison, if you want this to be done on the client side. Commented Jan 3, 2021 at 9:48
  • @leoperessoli, I mean, it seems like you've done it yourself in your own example. This: {% if email == email_conf %} is excalty what you are asking, comparing the 2 value with Python 's Jinja (Without JavaScritp= Commented Jan 3, 2021 at 9:58
  • If this don't work for you then you should specify better what you expect and why ` {% if email == email_conf %}`don't works as expected Commented Jan 3, 2021 at 9:59

1 Answer 1

0

Before be able to make what you want do which is to "Compare 2 HTML Forms" you should do the following:

  1. Make sure that you want to run the comparison (or display any other thing really) After the user clicked the submit button. Because if is before then this can be done with JavaScript only, but if is after the submit button then go to step 2.

  2. You need to 'save' the state of the form From Flask's server back to the browser (Server Side ---> Client Side). I prepare a simple App in order to show you this:

1. Add this to app.py

from flask import Flask,request, render_template app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': print('here') email_1 = request.form['email'] email_2 = request.form['email_conf'] return render_template('index.html', email_1=email_1, email_2=email_2) return render_template('index.html') if __name__ == '__main__': app.run(debug=True) 

2. Add this to template/index.html

NOTE: You had the closing tag in the input, is not needed as input are auto closing tags

<form action="/" method="POST"> <input type="email" name="email"> <input type="email" name="email_conf"> <input type="submit" value = "Entrar"> </form> <!-- Show the Output --> {{email_1}} {{email_2}} <!-- Do wathever you want with that --> {% if email_1 and email_2 %} {% if email_1 == email_2 %} <p>My email is {{email_1}} and I put the same as {{email_2}}</p> {% elif email_1 == '[email protected]'%} <p>{{email_1}} likes Python</p> {% for word in email_1 %} <option >{{word}}</option> {% endfor%} {% else %} <p>Something wrong with {{email_1}} maybe not match this-> {{email_2}}??</p> {% endif %} {% endif %} 

Documentation

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.