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!

  • Basically you want to make the comparison Before the User hits the submit buttom or Before? – Federico Baù Jan 03 '21 at 09: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! – leoperassoli Jan 03 '21 at 09: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. – Abe Malla Jan 03 '21 at 09: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= – Federico Baù Jan 03 '21 at 09: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 – Federico Baù Jan 03 '21 at 09:59
  • @AbeMalla thanks for your response! I know it would be easir using js but this must be done just with Flask and HTML. – leoperassoli Jan 03 '21 at 10:23
  • @FedericoBaù I think the comparison isn't working because the variables email and email_conf are empty. I took the if statement out of the form tag and tried to show the variables values: `{% if email == email_conf %}

    {{email}}

    {% endif %}` however even when the emails are the same nothing is showed on the screen.
    – leoperassoli Jan 03 '21 at 10:29

1 Answers1

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 == 'eggs@spam.com'%}
            <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

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