w4g
w4g

Reputation: 117

How to select multiples checkboxes and insert in MySql with Flask

I have the following code in HTML. Once the user selects the checkbox of the exams and clicks the button, each value of the ones that have been filled will be registered as "row" in column X. Eg: Example: User selected the checkbox with value "glicemia", then it will be registered in a column with the name "nomeexame" with the value "glicemia".

That way, I would like to know what the code to put on the function page would look like (it's at the end).

cadastrar_checkbox.html

{% extends "layout.html" %}


{% block content %}


<center>
<form method="POST" action="/preenche-exames">
<div class="container-fluid">


      <div class="card" style="margin:50px 0">
      <!-- Default panel contents -->
        <div class="card-header"><b>Doenças Endócrinas</b></div>

        <ul class="list-group list-group-flush">
        <li class="list-group-item">Glicemia
          <label class="switch ">
          <input type="checkbox" name="check" value="glicemia" class="danger">
          <span class="slider round"></span>
          </label>
        </li>
        <li class="list-group-item">Teste oral de Tol. Glicose
          <label class="switch ">
          <input type="checkbox" name="check" value="teste_oral_tol_glic" class="danger">
          <span class="slider round"></span>
          </label>
        </li>
        <li class="list-group-item">Insulina
          <label class="switch ">
          <input type="checkbox" name="check" value="insulina" class="danger">
          <span class="slider round"></span>
          </label>
        </li>
        <li class="list-group-item">Glicose em Jejum
          <label class="switch ">
          <input type="checkbox" name="check" value="glicose_jejum" class="danger">
          <span class="slider round"></span>
          </label>
        </li>

        </ul>

      </div>

</div>
<button type="submit" class="btn btn-danger">Continuar</button>
</form>

</center>


{% endblock content %}

E minha página em python das configurações:

@app.route('/testezinho')
def hedllo():
    return render_template('cadastrar_checkbox.html')

Upvotes: 1

Views: 1178

Answers (2)

Christian Heath
Christian Heath

Reputation: 221

There are a few different ways depending on how you would like your data displayed. You can get all of the checkbox values in a list via

request.form.getlist('check')

or alternatively you could concatenate the results of your checkboxes in your server.

Upvotes: 1

Dinko Pehar
Dinko Pehar

Reputation: 6061

To get all checkboxes that are checked(ticked with checkmark) you can use:

request.form.getlist('check')

where 'check' is name attribute of checkbox tag.

It will return list of values.

Upvotes: 1

Related Questions