-1

I'm stuck trying to populate a HTML form, selecting the company from a dropdown, similar result to this, in SQL and Flask.

I have no clue which way to go, to bring a value from <option> in HTML back to Flask file.

Here are some samples of my code.

Flask Sample:

@app.route("/profile" ,  methods=['GET', 'POST'])
def profile():

if request.method == "GET":
    querieCompanies = db.execute("SELECT name FROM companies ORDER BY name ASC")
    querieAll = db.execute("SELECT * FROM companies WHERE id = ?", ??WHATGOESHERE??).fetchall()

return render_template("profile.html", querieCompanies=querieCompanies, querieAll=querieAll)

HTML Sample:

{% extends "layout.html" %}

{% block main %}


<form name="listprofile" method="get">
    <div class="mb-3">
        <h5>Select company</h5>
        <select class="mx-auto w-auto" name="dropdownmenu">
            <option disabled selected>Companies</option>
            {% for companies in querieCompanies %}
                <option value="{{ companies[0] }}">{{ companies[0] }}</option>
            {% endfor %}
        </select>
    </div>
<div class="container">

<div class="row">
    <div class="col mb-3 p-1 d-inline-bloc">
        <h5>Name</h5>
        {% for data in querieAll %}
            ??WHAT GOES HERE??
        {% endfor %}
   
       

Also, it seems i can't populate <input>, i'm able to populate a <textbox>, is there another solution?

1 Answer 1

1

Use request.args for getting data from get-method form

if you using sqlalchemy, then use colon-params in db.execute()

from flask import request
querieAll = db.execute("SELECT * FROM companies WHERE id = :value", {
    'value': request.args.get('dropdownmenu')
}).fetchall()
  1. In template for-loop, data is a tuple with companies columns data.
Sign up to request clarification or add additional context in comments.

Comments

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.