Tag Archives: submit multiple checkbox values

How to submit multiple checkbox values with php

Hello World, Welcome to ProjectsPlaza.com. Today we will learn how to submit and print multiple checkbox values with PHP. I have divided this tutorial into the following parts.
  • Create a form with multiple checkboxes
  • Submit the form with PHP and print the values

Create a form with multiple checkboxes

  • Create a form.php file and add the following in this.
  • <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Submit Checkbox Values with Php</title>
    </head>
    <body>
        <h3>Select Your Favorite Fruits</h3>
        <form action="" method="post">
            <ol>
                <li>
                    <input type="checkbox" name="fruit[]" value="apple" id="apple" />
                    <label for="apple">Apple</label>
                </li>
                <li>
                    <input type="checkbox" name="fruit[]" value="banana" id="banana" />
                    <label for="banana">Banana</label>
                </li>
                <li>
                    <input type="checkbox" name="fruit[]" value="grapes" id="grapes" />
                    <label for="grapes">Grapes</label>
                </li>
                <li>
                    <input type="checkbox" name="fruit[]" value="kivy" id="kivy" />
                    <label for="kivy">Kivy</label>
                </li>
            </ol>
            <input type="submit" name="submit" />
        </form>
    </body>
    </html>
  • You have noticed that we have added checkbox name as an array, It will store the value in array form. This is the important step. Once you added the values as array form, It will very easy to print the data.


Submit the form with PHP and print the values

Add the following code at the top of file form.php
<?php
if(isset($_POST['submit'])){
    $fruits=$_POST['fruit'];
    echo '<pre>';
    print_r($fruits);
    echo '</pre>';
}
?>