All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Display Checked Checkbox Value In PHP

Last Updated : Mar 11, 2024

How To Display Checked Checkbox Value In PHP

In this tutorial we will show you the solution of how to display checked checkbox value in PHP, for better understanding, let’s assume we have some checkboxes that contains a list of colors, courses, sweets, dishes, etc. and we ask user to check his/her favorites from them.

After this selection, we also have to save answers in Database because we have to use this information for another survey.

So, let us see how to display checked checkboxes value in PHP. whereas the concept of database is used later.

Step By Step Guide On How To Display Checked Checkbox Value In PHP :-

Now, to get the values of the checked checkboxes in PHP is very easy.

There are many ways with help of which we can do this. Let us understand one from them with the help of example given below.

<?php
if(isset($_POST['submit'])){
    if(!empty($_POST['fruits'])) {
        foreach($_POST['fruits'] as $value){
            echo "Chosen Fruits are : ".$value.'<br/>';
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
   <title> how to get checkbox value in PHP </title>
</head>
<body>
<form method="post" action="">
    <span> Which is your favorite fruits, you can choose more than one also ?</span>
<br/>
    <input type="checkbox" name='fruits[]' value="apple"> APPLE <br/>
    <input type="checkbox" name='fruits[]' value="banana"> Banana <br/>
    <input type="checkbox" name='fruits[]' value="mango"> Mango <br/>
    <input type="checkbox" name='fruits[]' value="guava"> Guava <br/>
 <br/>
    <input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
  1. As, here we see that we that in above example we show you an example in which HTML and PHP codes are used.
  2. Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
  3. After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
  4. Now, next is our title tag which defines the title of the webpage. The tag which has its closing tag is known as a paired tag. So, this is again a paired tag.
  5. Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
  6. Now, as above we see we first create a form and after that inside form, we just use input having type checkbox. With help of this we create some checkboxes and a submit button.
  7. At next inside php codes we see that we use foreach loop to get the required checkboxes values. And using echo we display values on screen.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to display checked checkbox value in PHP.

I hope this tutorial on how to display checked checkbox value in PHP helps you and the steps and method mentioned above are easy to follow and implement.