I used this tutorial to build a flask form with multiple checkboxes, grouped into several groups on a web page.
The app takes the values from checked checkboxes and shows them on the screen.
application.py:
from flask import Flask, request
....
@app.route('/index')
@login_required
def dashboard():
return render_template('index.html', name=current_user.username)
@app.route("/output", methods = ["GET", "POST"] )
def process_form():
formData = request.values if request.method == "GET" else request.values
response = "Output: <pre>%s</pre>" % "<br/>\n".join(["%s:%s" % item
for item in formData.items()] )
return response
....
index.html:
<form action = "/output" method="POST">
<table>
<thead>
<tr>
<th>Offer1</th>
<th>Offer2</th>
<th>Offer3</th>
<th>Offer4</th>
</tr>
</thead>
<tbody>
<tr>
<td><label><input type="checkbox" name="offer1[]" value="a" class="offer1" />a</label></td>
<td><label><input type="checkbox" name="offer2[]" value="a" class="offer2" />b</label></td>
<td><label><input type="checkbox" name="offer3[]" value="a" class="offer3" />a</label></td>
<td><label><input type="checkbox" name="offer4[]" value="a" class="offer4" />a</label></td>
</tr>
<tr>
<td><label><input type="checkbox" name="offer1[]" value="b" class="offer1" />b</label></td>
<td><label><input type="checkbox" name="offer2[]" value="b" class="offer2" />b</label></td>
<td><label><input type="checkbox" name="offer3[]" value="b" class="offer3" />b</label></td>
<td><label><input type="checkbox" name="offer4[]" value="b" class="offer4" />b</label></td>
</tr>
<tr>
<td><label><input type="checkbox" name="offer1[]" value="c" class="offer1" />c</label></td>
<td><label><input type="checkbox" name="offer2[]" value="c" class="offer2" />c</label></td>
<td><label><input type="checkbox" name="offer3[]" value="c" class="offer3" />c</label></td>
<td><label><input type="checkbox" name="offer4[]" value="c" class="offer4" />c</label></td>
</tr>
</table>
</form>
It only returns a first checked value for each group, e.g. if I check a and b under Offer1 group, and then b and c for Offer 2, it will only return a for Offer1 and b for Offer2.
How do I get it to return all checked values? How do I access each checked value in the offer1[], offer2[], offer3[] and offer4[]?
Aucun commentaire:
Enregistrer un commentaire