5

I have the following code for my form in HTML:

<div class="container-fluid" id="feedback">
    <p><h2>Give us your feedback on the cafe!</h2>
        <h4><em>Maximum of 150 Characters.</em></h4>
    </p>
    <form action="{{ url_for('feedback') }}" method="post">
        <div class="form-group">
            <label class="sr-only" for="fb">Feedback</label>
            <textarea class="form-control" id="fb" placeholder="Enter your Feedback here..." type="text" maxlength="150"></textarea>
        </div>
        <div class="form-group">
            <label class="sr-only" for="rating">Rating for Cafe</label>
            <select class="form-control" id="rating">
                <option>5</option>
                <option>4</option>
                <option selected>3</option>
                <option>2</option>
                <option>1</option>
            </select>
        </div>
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="postal_code" placeholder="Postal Code of Cafe" type="text"/>
        </div>
        <div class="form-group">
            <button class="btn btn-default" type="submit">Submit!</button>
        </div>
    </form>
</div>

I tried to use rating = request.form.get("rating") in my python code to obtain the value of "rating" from the form, however I was unable to get the value in rating at all. How can I get the selected value from the HTML form in python code?

1 Answer 1

6

Give your select a name, and your options a value each.

<select class="form-control" id="rating" name="rating">
    <option value="5">5</option>
    <option value="4">4</option>
    <option value="3" selected>3</option>
    <option value="2">2</option>
    <option value="1">1</option>
</select>

Your post request then would have a field "rating" with a value of for example "3".

1
  • @AbelAshine I don't understand your question, would you mind providing some context?
    – Blauelf
    May 27, 2021 at 9:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .