All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Get Checkbox Value In PHP

Last Updated : Mar 11, 2024

How To Get Checkbox Value In PHP

In this tutorial we will show you the solution of how to get checkbox value in PHP, let us understand this with help of example. As assume there is form and having some checkboxes to select hobbies, or the subjects which have completed and want to choose.

Like this there are many examples and we want to save the selected values in database for later use.

So, how to do this and our major question may be how to do this in PHP. As result of this confusion today we are going to show you how o gets checkbox value in PHP.

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

Now, to get the values of the checkboxes that we have selected in PHP is very easy.

There are many ways with help of which we can do this. And today here we are going to show you the most preferred way, let us understand this with help of example.

<?php
if(isset($_POST['submit'])){
    if(!empty($_POST['character'])) {
        foreach($_POST['character'] as $value){
            echo "Chosen Characters 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 marvel character, you can choose more than one also ?</span>
<br/>
    <input type="checkbox" name='character[]' value="iron_man"> Iron Man<br/>
    <input type="checkbox" name='character[]' value="caption"> Caption America<br/>
    <input type="checkbox" name='character[]' value="black_window"> Natasha <br/>
    <input type="checkbox" name='character[]' value="hulk "> Hulk <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. Here, as we see above that we create a form in which we create some checkboxes. After that we create a submit button.
  7. And after selecting checkbox when user click on submit button. Then our PHP codes comes in action. And with help of foreach loop our selected colors displayed on screen. For better understanding you can see the above codes quickly.

Conclusion :-

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

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪