Not printing my checked checkboxes [duplicate]

1

Good night, I created 3 checkboxes and I made an array method so when I click on send it shows me the checkboxes selected with your value , but this is not happening, I'm testing so that I put in my original form that I'm going to need to list my selected checkboxes ...

Can anyone help me?

html:

<html>
<head>
<title>Teste de checkbox</title>
</head>
<body>
    <h1>Teste de check</h1>

    <form method="post" action="form.php">

    <input type="checkbox" name="cor" value="azul">

    <input type="checkbox" name="cor" value="vermelho">

    <input type="checkbox" name="cor" value="amarelo">


    <input type="submit" name="olhar">

    </form>


</body>
</html>

form.php:

<?php 
if(isset($_POST["cor"])) {

    for($i = 0; $i < count($_POST["cor"]); $i++) {

        echo "a cor ".$_POST["cor"] [$i]." foi selecionada";

    }
}

?>

Thank you ...

    
asked by anonymous 23.12.2016 / 03:39

1 answer

3

You just have to "vector" your inputs, as you used the same name, and then want to recover the value of all, it needs to be this way:

<form method="post" action="form.php">

<input type="checkbox" name="cor[]" value="azul">

<input type="checkbox" name="cor[]" value="vermelho">

<input type="checkbox" name="cor[]" value="amarelo">


<input type="submit" name="olhar">

</form>

I hope I have helped !!!

    
23.12.2016 / 03:46