0

I've tried to apply what I saw in this answer and it's not working:

Getting value from select tag using flask

I don't see what's different about his situation, so I can't understand why mine won't work.

Here's my route:

@bp.route('/ecommerce/cart/add/product/<id>', methods=['GET', 'POST'])
@login_required
def ecommerceCartAdd(id):
    select = request.form.get('modal-product-quantity')
    print(str(select))
    p = stripe.SKU.retrieve(id)
    product = dict(
        id=p.id,
        prod_id=p.product,
        name=p.attributes.name,
        image=p.image,
        price=convert_price(p.price)
    )
    session['cart'].append(product)
    flash(f"[{product['name']}] added to cart", "info")
    return redirect(url_for('projects.ecommerce'))

And here's my HTML: (I'm including the JS I'm using to change the display just in case I somehow messed that up, though I can't see how.)

(Also, note that the HTML below is being put in the main page through a jinja2 include statement, but the modal is showing up properly, so I don't see how that could affect it either.

            <div class="modal-footer">
                <form class="form-inline" method="POST" action="{{ url_for('projects.ecommerceCartAdd', id=p.id) }}">
                    <div class="input-group">

                        <label class="mr-sm-2 float-left" for="prod-quantity-select">Quantity: </label>
                        <select class="custom-select float-left" id="prod-quantity-select" onchange="AdjustQuantity()"
                            name="modal-product-quantity">
                            <option selected value="1">1</option>
                            {% for i in range(2, 100) %}
                            <option value="{{ i }}">{{ i }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <script>

                        function AdjustQuantity() {
                            var q = document.getElementById("prod-quantity-select");
                            var quantity = q.options[q.selectedIndex].value;

                            document.getElementById("modal-price-label").innerHTML = 'Total: ';
                            document.getElementById("modal-price-counter").innerHTML = '(x' + quantity + ')';
                        }

                    </script>
                    <a href="{{ url_for('projects.ecommerceCartAdd', id=p.id) }}" class="btn btn-primary btn-sm"> Add to
                        cart
                    </a>
                </form>
                <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button>
            </div>

I've tried screwing around with where I put the action="url_for... and with putting the method="POST" in the select tag instead of the form tag.

No matter what I do, my output is None. And, I know that the route is not using the POST method because when I put the print statement in a conditional, if request.method="POST", it doesn't print--but like I said, otherwise it prints None.

I need help understanding how to post and access the data from the select field.

Thanks!!!

Peter Charland
  • 409
  • 6
  • 18

1 Answers1

1

There's an ahref pretending to be a button, so you are not submitting the form.

Try changing this:

<a href="{{ url_for('projects.ecommerceCartAdd', id=p.id) }}" class="btn btn-primary btn-sm"> Add to cart </a>

To this:

<button type="submit" class="btn btn-primary btn-sm"> Add to cart </button>
dmitrybelyakov
  • 3,709
  • 2
  • 22
  • 26