PHP Check Box

In this tutorial we are going to learn the usage of the PHP check box,  we will see how to take user input using the PHP check box but first let’s have a brief introduction to the check box.


Php check box form is a sort of button in an HTML form, as we on or off the button as it is we check or uncheck the check box, which means to say a check box in PHP works likewise a button.

A Php check box form is used when a developer wants a user to check an option from the given options. A checkbox form helps a user to check his or her desired option/options from the list of options.

For example, suppose you are filling an online admission form on a browser and there is a check box form of check language or check subjects and you have to choose an option from them then this mechanism of checking a single option or multiple options from a list of options is called a PHP check box form. 

Let’s take an example for better understanding.

Examples Of Check Box In PHP

Example 1

<form method="post" action="myyy.php">
    <span>Select a subject</span><br/>
    <input type="checkbox" name='sub[]' value="Software Engineering">Software Engineering<br/>
    <input type="checkbox" name='sub[]' value="Data Structures"> Data Structures<br/>
    <input type="checkbox" name='sub[]' value="Introduction to Programming"> Introduction to Programming <br/>
    <input type="checkbox" name='sub[]' value="Economics">Economics <br/>

    <input type="submit" value="Submit" name="submit">



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

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

echo '<script>alert("Please select a subject")</script>';    	

Code Explanation

In the above code, we created an HTML form and assigned it the “post” method and in action, we assigned the working file name.

We created four input type “check boxes” for selecting subjects, in the input fields we assigned a “sub[]” array to each input field name and assigned a subject name to each input field value.

After that, we created an input type button to submit the selections. Then we opened the PHP tag and used the isset function to check if the user has submitted the selections by clicking on the submit button or not.

When the submit button will be clicked the selected values will be stored in the values parameter and then an if condition will execute and check if it contains a value or not, if it is not empty then a for each loop will execute and will show the checked or selected values mean to say it will print our selected subject names.

the else part states that in case the user did not select any option or just clicked the submit button so nothing will be stored in the value and an alert will appear that will remind the user to select an option first.  

Let’s check the results.


After clicking the submit button without selecting an option.

As you can see the above output it is displaying the selected subjects’ names, and it is also displaying the alert box that appears when the submit button is clicked without selecting subjects.

Let’s move forward to our next example.

Example 2

<form action="index.php" method="post">
<input type="checkbox" name="Agree_for_using_cookies"  value="C">check the box to allow cookies.
<input type="submit" value="submit response">
If ($_POST['Agree_for_using_cookies']) {
echo ('<script>alert("using cookies")</script>');
	echo ('<script>alert("Please check the box")</script>');

Code Explanation

In the above code, we took an HTML form and created an input type checkbox form that will get permission from the user to use cookies. we also created an input type submit button.

In the PHP code, we declared a variable “$cookies_accepted” and set its value to false.

Then we used the if condition that states that if the user has checked the box so echo “using cookies” else an alert box will appear that will remind the user to “check the box”.

Check out the results below.


 After checking the box.

Without checking the box.


In today’s tutorial, we talked about the PHP checkbox form, first of all, we discussed an introduction to the checkbox.

Then we understood the concept of the PHP checkbox form by practicing a few examples. We also discussed their code with explanations and saw their outputs as well.

That’s all for today hope you like the tutorial if you have any queries regarding this tutorial so feel free to contact us below. Thanks for reading.

Suggested Article:

Leave a Comment