In this article, we will learn how to get multiple values of selected checkboxes in PHP, its very useful when you make multiple-choice questions.


Example

Here I am writing code for choosing your favorite programming languages, Select multiple checkboxes and get values of it


index.php



<!DOCTYPE html>
<html>
    <head>
        <title>Multiple checkbox</title>
        <style>

            body{
                background: #ccc;
            }

            .my-formcontainer{
                width: 800px;
                margin: 40px auto;
                background: #fff;
                padding: 20px;
            }
        </style>
    </head>
    <body>

        <div class="my-formcontainer">
            <p><strong>You have chosen</strong></p>
            <?php
            if (isset($_POST['submit'])) {
                if (!empty($_POST['checkArr'])) {
                    foreach ($_POST['checkArr'] as $checked) {
                        echo $checked . '<br>';
                    }
                } else {
                    echo 'Checkbox is not selected!';
                }
            }
            ?>


            <form action="" method="post">
                <h3>Choose your favorite programing language</h3>
                <div>
                    <input type="checkbox" name="checkArr[]" value="Laravel">
                    Laravel
                </div>
                <div>
                    <input type="checkbox" name="checkArr[]" value="Java">
                    Java
                </div>
                <div>
                    <input type="checkbox" name="checkArr[]" value="HTML">
                    HTML
                </div>
                <div>
                    <input type="checkbox" name="checkArr[]" value="CSS">
                    CSS
                </div>

                <div>
                    <br>
                    <input type="submit" name="submit" value="Choose languages" />
                </div>
            </form>

        </div>

    </body>
</html>


Output:





Are you facing problems in understanding this article?
You can watch the video. Watch it now