PHP and MySQL (55 Blogs) Become a Certified Professional

How to Create Checkboxes In PHP?

Published on Sep 27,2019 9.4K Views

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?

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:

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


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:

//Valid options . A whitelist of allowed options.
$vegetableOptions= array(
//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.
<form action=””method=”post”>
<input type=”checkbox” name=”vegetables[]” value=” Carrot”>
<input type=”checkbox” name=”vegetables[]” value=”Onions>
<input type=”checkbox” name=”vegetables[]” value=”Chillies”>
<input type=”submit” value=”Register”>

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.


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.