Save checkbox marked in an array!

Asked

Viewed 506 times

-1

I want, when selecting multiple checkbox, these values to be saved in an array, so that it is later saved in the database. If I leave the checkbox with the same id, as follows:

<div class="form-group">
          <label style="margin-bottom: 10px"> Dias disponíveis 
          </label><br />
            <label><input type="checkbox" name="dias" id="dias" 
value="Segunda-Feira">  Segunda-Feira</label><br />
            <label><input type="checkbox" name="dias" id="dias" 
value="Terça-Feira">  Terça-Feira</label><br />
            <label><input type="checkbox" name="dias" id="dias" 
value="Quarta-Feira">  Quarta-Feira</label><br />
            <label><input type="checkbox" name="dias" id="dias" 
value="Quinta-Feira">  Quinta-Feira</label><br />
            <label><input type="checkbox" name="dias" id="dias" 
value="Sexta-Feira">  Sexta-Feira</label>
          </div>

php already understands and places the marked options in the array directly after Submit using:

$chgeckboxes = $_POST['checkbox'];

Anyone can help?

  • Do not put ids with the same name on a page. put classes.

  • Using classes php understands the same way and saves the values in the array?

  • 1

    No, don’t use it id repeated, it makes no sense. Just add you [] at the name of the fields, making name="dias[]". This way, PHP will receive a array with the selected days.

  • Got it, then I can take the ids of all the checkers?

  • Yes, can and should.

  • Thank you so much for your help, I’ll try to do it that way!!

  • Arthur, just having an id, it is not right to put multiple ids with the same name on a page. This is done with class, because the id is unique.

  • In that case you don’t need the id.

  • Understood, never use ids with the same name again, I really appreciate the help!!

  • @When you select the checkbox and select Ubmit, PHP already puts the values in an array, and that’s not right ? I didn’t quite understand your doubt.

  • My question was whether I was right

Show 6 more comments

1 answer

0

As already said, you should not use elements with the same id in a document.

In your case the correct thing is to use a pair of brackets [] at the end of the attribute name name="dias[]". This way the elements of the same name are treated as an array.

HTML

<form action="" method="post"> 
<div class="form-group">
    <label style="margin-bottom: 10px"> Dias disponíveis 
    </label><br />
    <label><input type="checkbox" name="dias[]" value="Segunda-Feira">  Segunda-Feira</label><br />
    <label><input type="checkbox" name="dias[]" value="Terça-Feira">  Terça-Feira</label><br />
    <label><input type="checkbox" name="dias[]" value="Quarta-Feira">  Quarta-Feira</label><br />
    <label><input type="checkbox" name="dias[]" value="Quinta-Feira">  Quinta-Feira</label><br />
    <label><input type="checkbox" name="dias[]" value="Sexta-Feira">  Sexta-Feira</label>
    <input type="submit">
</div>
</form>

And in PHP

if(!empty($_POST['dias']) && count($_POST['dias']) ){
   $chgeckboxes = $_POST['dias'];
   //print_r($chgeckboxes);


}

Browser other questions tagged

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