How to submit multiple checkbox values with php

Hello World, Welcome to 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">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Submit Checkbox Values with Php</title>
        <h3>Select Your Favorite Fruits</h3>
        <form action="" method="post">
                    <input type="checkbox" name="fruit[]" value="apple" id="apple" />
                    <label for="apple">Apple</label>
                    <input type="checkbox" name="fruit[]" value="banana" id="banana" />
                    <label for="banana">Banana</label>
                    <input type="checkbox" name="fruit[]" value="grapes" id="grapes" />
                    <label for="grapes">Grapes</label>
                    <input type="checkbox" name="fruit[]" value="kivy" id="kivy" />
                    <label for="kivy">Kivy</label>
            <input type="submit" name="submit" />
  • 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
    echo '<pre>';
    echo '</pre>';

Leave a Reply

Your email address will not be published. Required fields are marked *