❮ HTML <input> type attribute
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
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:
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.
Recommended videos for you
Service-Oriented Architecture With Java
Watch Now
Building Application With Ruby On Rails Framework
Watch Now
A Day In The Life Of A Node.js Developer
Watch Now
Spring Framework : Introduction to Spring Web MVC & Spring with BigData
Watch Now
Learn Perl-the Jewel of Scripting Languages
Watch Now
MS .Net – An IntelliSense Way of Web Development
Watch Now
Microsoft Sharepoint 2013 : The Ultimate Enterprise Collaboration Platform
Watch Now
Node JS Express: Steps to Create Restful Web App
Watch Now
Portal Development and Text Searching with Hibernate
Watch Now
Building Web Application Using Spring Framework
Watch Now
Node JS : Steps to Create Restful Web App
Watch Now
Effective Persistence Using ORM With Hibernate
Watch Now
PHP & MySQL : Server-side Scripting Language for Web Development
Watch Now
Microsoft .NET Framework : An IntelliSense Way of Web Development
Watch Now
Recommended blogs for you
Know About Parameterized Constructor In Java With Examples
Read Article
Java Applet Tutorial – Know How to Create Applets in Java
Read Article
How To Create Library Management System Project in Java?
Read Article
Top Java Interview Questions You Must Prepare In 2022
Read Article
What are the different types of Classes in Java?
Read Article
Everything You Need To Know About Unserialize In PHP
Read Article
Top MVC Interview Questions and Answers You Need to Know in 2022
Read Article
What is the Average Salary of a Front End Developer?
Read Article
Java Programs for Practice: Know the Simple Java Programs for Beginners
Read Article