Chained Dropdown in flask, get data from sqlite database
In your head
, add a handler for changing the values of the selects:
<script type="text/javascript"> $("#select_county").change(function() { $.ajax({ type: "POST", url: "{{ url_for('select_county') }}", data: { county: $("#select_county").val() }, success: function(data) { $("#select_city").html(data); } }); });</script>
This uses jquery.change to detect when the county select is changed and jquery.ajax to send that selected value to your backend, which would be something like:
@app.route('/select_county', methods=['POST'])def select_county(): ret = '' for entry in ...your_database_query...: ret += '<option value="{}">{}</option>'.format(entry) return ret
This list of <option>
tags is then inserted into the second select through jquery.html.