Save to Checkbox Database

Asked

Viewed 155 times

0

I am in doubt on how to save a checkbox in the database. How to save a sequence of numbers to know which options have been selected. The last selected option is saved in the current form. Is there any way to save as "1234" or "24" for example to know the options the user has selected?

<div class="row narrow p-0">
<div class="col-12">
    <div class="form-group">
        <label class="star" for="dias_selecionados">Quais dias deseja?</label><label class="text-danger" >  *Somente os dias desejados</label>
        <div class="mt-1 py-1 px-2 border border-festival rounded">
            <div class="form-check form-check-inline">
                <input class="form-check-input m-0" type="checkbox" name="dias_selecionados" id="1" value="1" autocomplete="off"/>
                <label class="form-check-label" for="1">dia 1</label>
                <?php if (isset($_POST['dias_selecionados']) && $_POST['dias_selecionados'] == '1')?>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input m-0" type="checkbox" name="dias_selecionados" id="2" value="2" autocomplete="off"/>
                <label class="form-check-label" for="2">Dia 2</label>
                <?php if (isset($_POST['dias_selecionados']) && $_POST['dias_selecionados'] == '2')?>

            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input m-0" type="checkbox" name="dias_selecionados" id="3" value="3" autocomplete="off"/>
                <label class="form-check-label" for="3">Dia 3</label>
                <?php if (isset($_POST['dias_selecionados']) && $_POST['dias_selecionados'] == '3')?>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input m-0" type="checkbox" name="dias_selecionados" id="4" value="4" autocomplete="off"/>
                <label class="form-check-label" for="4">Dia 4</label>
                <?php if (isset($_POST['dias_selecionados']) && $_POST['dias_selecionados'] == '4')?>
            </div>
        </div>
    </div>
</div>
</div>
  • Evaluate the use of the data type json.

2 answers

0


The easiest would be for you to create a column in the database to represent the selection of each check box.

Otherwise, if you don’t have the option to create a column in the BD you could adopt a binary combination.

0000 - No check box selected 0001 - First check box selected 0010 - Selected second check box 0011 - Selected first and second check box 0100 - Selected third check box ... and so on

0

So... puts a class in the checkbox, same for everyone and recovers the value (id).

In javascript do so when sending to php via post.

<input class="form-check-input m-0 dias_selecionados" type="checkbox" name="dias_selecionados" id="1" value="1" autocomplete="off"/>

var check_lista = [];
// aqui vc sabe quantos checks dessa classe tem para verificar, e se estiver com Check, então, recupera o valor ou o ID
for(var i=0; i < $('.dias_selecionados:checked').length; ++i){
    check = $('.dias_selecionados:checked')[i].value;
    check_lista.push(check);
}

so vc keeps in an array using push, or if you think better, you can concatenate into a String. Ai is with you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.