How can i get checkbox in php?

❮ HTML <input> type attribute

Show

Example

Let the user select one or more options of a limited number of choices:

<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label><br>

Try it Yourself »


Definition and Usage

The <input type="checkbox"> defines a checkbox.

The checkbox is shown as a square box that is ticked (checked) when activated.

Checkboxes are used to let a user select one or more options of a limited number of choices.

Tip: Always add the <label> tag for best accessibility practices!


Browser Support

Attribute
type="checkbox" Yes Yes Yes Yes Yes

Syntax


❮ HTML <input> type attribute


Web-users associate checkboxes with “multiple choices”. It helps you choose between one of the two mutually exclusive options. It is like answering a question with either with yes or no. In this article, we will see how to create single and multiple Checkboxes in PHP in the following sequence:

  • What are Checkboxes?
  • Single Checkboxes in PHP
  • Multiple Checkboxes in PHP

What are Checkboxes?

A checkbox helps a user to choose between one of the two mutually exclusive options. There are two types of checkboxes based on the number of options that you make.

  • Checkbox group 
  • Checkbox

Clicking on the checkbox will either change it to ON to OFF or vice verse. These are preferred for multiple choices options. In use cases where a SINGLE choice must be made, a radio button or a select option will work better. If in an instance “check all that apply” question comes up at such cases checkboxes group can be used.

Now that you know what are checkboxes, let’s move on and see how to create a single checkbox in PHP.

Single Checkbox in PHP

Each response should be toggled individually to ON and OFF. The single checkbox example is as shown below:

<?php
//Set “terms accepted” to false by default.
$termsAccepted=false;
//If the POST variable “terms_of_services” exists.
If ($_POST[‘terms_of_services’]) {
//Checkbox has been ticked.
$termsAccepted=true;
}
//Print it out for example purposes.
If($termsAccepted){
Echo ‘Terms of Service checkbox ticked!’;
}
?>
<form action=””method=”post”>
<lable>
I accept the terms of service.
<input type=”checkbox” name=”terms_of_services” value=”Y”>
<lable>
<input type=”submit” value=”Register”>
</form>

Next, we will see how to create multiple checkbox in PHP.

Multiple Checkbox in PHP

Multiple checkbox is a little more complex than a single checkbox. If the user wants to select one or more options than this is used.
In this example, we allow our users to select their favourite vegetables:

<?php
//Valid options . A whitelist of allowed options.
$vegetableOptions= array(
‘tomato’,
‘carrot’,
‘onion’,
‘Chilly’
);
//Empty array by default.
$vegetables = array();
//If the POST variable “vegetables” is a valid array.
If(!empty($_POST[‘vegetables’]) && is_array($_POST[‘vegetables’])){
//Loop through the array of checkbox values.
Foreach($_POST[‘vegetables’] as $vegetables){
//Make sure that this option is a valid one.
If(in_array($vegetable, $vegetableOptions)){
//Add the selected options to our $vegetables array.
$vegetables[]=$vegetable;
}
}
}
Var_dump($vegetables);
?>
<form action=””method=”post”>
<label>
Tomatoes
<input type=”checkbox” name=”vegetables[]” value=” Carrot”>
<label>
<label>
Onions
<input type=”checkbox” name=”vegetables[]” value=”Onions>
</label>
<label>
Chillies
<input type=”checkbox” name=”vegetables[]” value=”Chillies”>
</label>
<input type=”submit” value=”Register”>
</form>

With this, we have come to the end of our article. I hope you understood what are checkboxes and how to create them in PHP.

If you found this blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”Checkboxes in PHP” and I will get back to you.

How can i get checkbox in php?

Service-Oriented Architecture With Java

Watch Now

How can i get checkbox in php?

Building Application With Ruby On Rails Framework

Watch Now

How can i get checkbox in php?

A Day In The Life Of A Node.js Developer

Watch Now

How can i get checkbox in php?

Spring Framework : Introduction to Spring Web MVC & Spring with BigData

Watch Now

How can i get checkbox in php?

Learn Perl-the Jewel of Scripting Languages

Watch Now

How can i get checkbox in php?

MS .Net – An IntelliSense Way of Web Development

Watch Now

How can i get checkbox in php?

Microsoft Sharepoint 2013 : The Ultimate Enterprise Collaboration Platform

Watch Now

How can i get checkbox in php?

Node JS Express: Steps to Create Restful Web App

Watch Now

How can i get checkbox in php?

Portal Development and Text Searching with Hibernate

Watch Now

How can i get checkbox in php?

Building Web Application Using Spring Framework

Watch Now

How can i get checkbox in php?

Node JS : Steps to Create Restful Web App

Watch Now

How can i get checkbox in php?

Effective Persistence Using ORM With Hibernate

Watch Now

How can i get checkbox in php?

PHP & MySQL : Server-side Scripting Language for Web Development

Watch Now

How can i get checkbox in php?

Microsoft .NET Framework : An IntelliSense Way of Web Development

Watch Now

How can i get checkbox in php?

Know About Parameterized Constructor In Java With Examples

Read Article

How can i get checkbox in php?

Java Applet Tutorial – Know How to Create Applets in Java

Read Article

How can i get checkbox in php?

How To Create Library Management System Project in Java?

Read Article

How can i get checkbox in php?

Top Java Interview Questions You Must Prepare In 2022

Read Article

How can i get checkbox in php?

What are the different types of Classes in Java?

Read Article

How can i get checkbox in php?

Everything You Need To Know About Unserialize In PHP

Read Article

How can i get checkbox in php?

Top MVC Interview Questions and Answers You Need to Know in 2022

Read Article

How can i get checkbox in php?

What is the Average Salary of a Front End Developer?

Read Article

How can i get checkbox in php?

Java Programs for Practice: Know the Simple Java Programs for Beginners

Read Article

How can I get checkbox data in PHP?

In order to get the checkbox values, you can use the foreach() method. Note: The foreach() method functions by looping through all checked checkboxes and displaying their values.

How can I get checkbox data?

You can also use the below code to get all checked checkboxes values..
<script>.
document.getElementById('btn').onclick = function() {.
var markedCheckbox = document.querySelectorAll('input[type="checkbox"]:checked');.
for (var checkbox of markedCheckbox) {.
document.body.append(checkbox.value + ' ');.
</script>.

How check if checkbox is checked PHP?

Read if Checkbox Is Checked in PHP.
Use the isset() Function on $_POST Array to Read if Checkbox Is Checked..
Use the in_array() Function to Read if the Checkbox Is Checked for Checkboxes as an Array..
Use the isset() Function With Ternary Function to Read if the Checkbox Is Checked..

How do you create a checkbox?

The simplest way to create a checkbox in HTML is by using the input tag. We have set the input type to “checkbox” as you can see in the example code. The name attribute lets us give a name to the checkbox, and with the value attribute, we specify the value it holds.