How to Get Checkbox Values in PHP


In this article, you will learn how to get the checkbox values in PHP.

Let’s say you have 4 checkboxes of colours.

Get Checkbox Value in PHP

In order to get the checkbox values, you can use the foreach() method.

<html>
<body>

<form method="post" action="">
    <span>What are your favourite colours?</span><br/>
    <input type="checkbox" name='colour[]' value="Red"> Red <br/>
    <input type="checkbox" name='colour[]' value="Green"> Green <br/>
    <input type="checkbox" name='colour[]' value="Blue"> Blue <br/>
    <input type="checkbox" name='colour[]' value="Black"> Black <br/>
	<br/>
    <input type="submit" value="Submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])){

    if(!empty($_POST['colour'])) {

        foreach($_POST['colour'] as $value){
            echo "Chosen colour : ".$value.'<br/>';
        }

    }

}
?>

</body>
</html>

Result

Note: The foreach() method functions by looping through all checked checkboxes and displaying their values.


Share on social media

//