0

I have a credit card validation form. I want to get the class name of a highlighted credit card type image in the routes.py file.

{% extends 'layout.html' %}
{% block content %}
<form class="form" method="post" action="{{ url_for('checkout') }}">
    <div class="creditCardForm">
            <div class="heading">
                <h2>Confirm Purchase</h2>
            </div>
            <div class="payment">
                    <div class="form-group owner">
                        <label for="owner">Owner</label>
                        <input type="text" class="form-control" id="owner" name="owner">
                    </div>
                    <div class="form-group CVV">
                        <label for="cvv">CVV</label>
                        <input type="text" class="form-control" id="cvv" name="cvv">
                    </div>
                    <div class="form-group" id="card-number-field">
                        <label for="cardNumber">Card Number</label>
                        <input type="text" class="form-control" id="cardNumber" name="cardNumber">
                    </div>
                    <div class="form-group" id="expiration-date">
                        <label>Expiration Date</label>
                        <select name="expiration-month">
                            <option value="01">January</option>
                            <option value="02">February </option>
                            <option value="03">March</option>
                            <option value="04">April</option>
                            <option value="05">May</option>
                            <option value="06">June</option>
                            <option value="07">July</option>
                            <option value="08">August</option>
                            <option value="09">September</option>
                            <option value="10">October</option>
                            <option value="11">November</option>
                            <option value="12">December</option>
                        </select>
                        <select name="expiration-year">
                            <option value="16"> 2020</option>
                            <option value="17"> 2021</option>
                            <option value="18"> 2022</option>
                            <option value="19"> 2023</option>
                            <option value="20"> 2024</option>
                            <option value="21"> 2025</option>
                        </select>
                    </div>
                    <div class="form-group" id="credit_cards">
                        <img src="{{ url_for('static', filename='pics/'+'visa.jpg') }}" id="visa" name="visa" class="">
                        <img src="{{ url_for('static', filename='pics/'+'mastercard.jpg') }}" id="mastercard" name="mastercard" class="transparent">
                        <img src="{{ url_for('static', filename='pics/'+'amex.jpg') }}" id="amex" name="amex" class="transparent">
                    </div>
                    <div class="form-group" id="pay-now">
                        <button type="submit" class="btn btn-info" id="confirm-purchase">Pay & Proceed</button>
                    </div>
            </div>
        </div>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
<script type="text/javascript" , src="{{ url_for('static', filename='script.js') }}"></script>
<script type="text/javascript" , src="{{ url_for('static', filename='jquery.payform.min.js') }}"></script>
{% endblock content %}

From the above page, if I enter VISA card details, the class name of the images "mastercard" and "amex" will change to transparent.

I want to verify that in my routes.py file. Hence I am looking for something like below:

@app.route("/checkout", methods=['POST'])
def checkout():
visa_class = request.form['visa'].getattr('class')
4
  • Did you solve this question yet? I have a similar trouble getting the position of elements from HTML using Flask. Commented Jan 6, 2021 at 17:39
  • You have no input elements in your div form-group. Commented Jan 7, 2021 at 16:44
  • You should also post your JavaScript code. Commented Jan 7, 2021 at 17:21
  • I think that the css class is not send to the server, only the id attribute. You hence might have to implement your own logic for that via JavaScript. Commented Jan 7, 2021 at 19:04

3 Answers 3

2

I found an alternative on my own today: I assigned the value to an invisible textarea, so that we can access it when using flask.

Codes are like following: HTML:

<form method="post" action="{{ url_for('main') }}" id="form">
  <div class="form-group">
    <textarea name="hidden" class="form-control" style="display: none;" value="" id="hidden"></textarea> 
  </div>
  <input type="submit" value="Confirm" onclick="Assign()" align="center">
</form>
<script>
function Assign()
{
  document.getElementById("hidden").value = "some values";
}

Python:

@app.route('/', methods=['GET', 'POST'])
def main():
    if request.method == 'POST':
            value_you_want = request.form['hidden']
Sign up to request clarification or add additional context in comments.

Comments

2

Solution No (2): Get the class of each card image

In this solution, the class of visa,mastercard and amex will be passed by POST method upon submitting the form via input fields visa_class,mster_card_class and amex_class respectively. These fields will be created upon submitting.

This is done by adding the following javascript/Jquery to the end of the HTML file before {% endblock content %}. An event on submitting the form will be added, after finishing rendering the whole page. I assume that there is only one form in the HTML, otherwise, form-id should be used.

<script>
    $(document).ready(function() {

        $("form").submit( function(eventObj) {

          $("<input />").attr("type", "hidden")
              .attr("name", "visa_class")
              .attr("value", $("#visa").attr('class'))
              .appendTo("form");

          $("<input />").attr("type", "hidden")
              .attr("name", "mster_card_class")
              .attr("value", $("#mastercard").attr('class'))
              .appendTo("form");

          $("<input />").attr("type", "hidden")
              .attr("name", "amex_class")
              .attr("value", $("#amex").attr('class'))
              .appendTo("form");

          return true;
        });
    })
</script>

THen classes values is obtained in checkout() as follows:

@app.route("/checkout", methods=['POST'])
def checkout():
    if request.method == 'POST':
        visa_class = request.form['visa_class']
        mster_card_class = request.form['mster_card_class']
        visa_amex_classclass = request.form['amex_class']
    else:
        visa_class = None # not yet
        mster_card_class = None
        visa_amex_classclass = None

Solution No (1): Identify Card Type in python

Here, I suggest another way to achieve your target, which is obvious from your words:

I want to verify that in my routes

This target is identification the card type

also, we find in the question:

if I enter VISA card details, the class name of the images "mastercard" and "amex" will change to transparent.

This mentions that in the HTML file there is a javascript event that takes care of identification the card type. This event is responsible for making only the correct card-type image appeared.

So, If we follow the same approach of that javascript event in python, we can check the card type from the card details, from a search on the Internet we find these piece of information[1]:

The first digit is different for each card network:

Visa cards – Begin with a 4 and have 13 or 16 digits

Mastercard cards – Begin with a 5 and has 16 digits***

American Express cards – Begin with a 3, followed by a 4 or a 7 has 15 digits

so in your python code, we can identify the card type from card number with a code like the following:

@app.route("/checkout", methods=['POST'])
def checkout():
    if request.method == 'POST':
        cardNumber= request.form['cardNumber']
        if cardNumber[0]=='4'and len(cardNumber) in (13,16):
            card_type='visa'
        elif cardNumber[0]=='5' and len(cardNumber)=16:
            card_type='mastercard'
        elif cardNumber[:1] in ('34','37') and len(cardNumber)=15:
            card_type='amex'
        else:
            card_type= None

        if card_type is None:
            '''
            taks some action
            ''''

It is so easy to verify the method if you check the javascript event related to the card type identification. Hence, you can make both Python and Javascript are identical, with no need to check the class.

Good Luck.

Comments

0

If u want to pass the “class” HTML attribute, u have to use class_='' since “class” is a reserved keyword in Python.

{{ form.searchCity(size=20, class_='searchCity') }}  

Documentation is available at :

http://wtforms.readthedocs.io/en/latest/crash_course.html#rendering-fields

However, the real power comes from rendering the field with its call() method. By calling the field, you can provide keyword arguments, which will be injected as html attributes in the output:

form.content(style="width: 200px;", class_="bar")
<input class="bar" id="content" name="content" style="width: 200px;" type="text" value="foobar" />'

1 Comment

Hello, I am not looking for enabling or disabling a button using flask. I am looking for code where I can the id or class or any html attribute of an html element using flask

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.