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:
<?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.