How can I save checkbox in bank with php?

1

I have several checkbox components on the site, each with the value of the name of a movie, how can I save it to the bank?

HTML of one of them:

 <input type="checkbox" class="checkbox" name="filme">

Do you have to have value or name different from each other? Type checkbox of the movie The Origin and another one of the Insurgent film?

It's for the user to save and watch the movies they've watched later, so it has to be checked

    
asked by anonymous 10.09.2015 / 01:29

1 answer

1

You need to put something like filme[] in the name of the checkboxes, and then make a foreach , as was said by @ AndréBaill.

Something like this:

<input type="checkbox" name="filme[]" value=filme_1>
<input type="checkbox" name="filme[]" value=filme_2>
<input type="checkbox" name="filme[]" value=filme_3>
<input type="checkbox" name="filme[]" value=filme_4>

And then in your php code:

<?php

if(isset($_POST['filme'])){
    $listaCheckbox = $_POST['filme'];

    foreach ($listaCheckbox as $filme) {
        echo $filme;
        //aqui você salva no seu banco
    }
}

?>

Remembering that before using a value coming from a POST, you must first validate it for security reasons, especially if you are going to save this data in the database. Take a look at the filter_input method.

    
10.09.2015 / 12:40