How to Get Values from Checkboxes & Radios in PHP

We mostly use HTML web forms while getting the input from the users, and we store the users’ information inside a database and for that we use PHP & MySQL. An HTML <form> tag may have multiple <input> elements and each <input> element has a different or same type attribute, you can easily get values from a <input> tags which has a type of text, but if you are facing difficulty while getting the values from <input type=”radio”> or <input type=”checkbox”> then you are here to learn how to do it easily. Even I’ll teach you about getting values from multiple checkboxes at once. So carry on reading and learn this thing today.



Before moving forward, you’d love to read these tutorials:

How to get value from a checkbox?

When you are dealing with an <input> tag having a type=checkbox then you can use the following code to get the input entered by the user:

<form method=”post” action=”page.php”><input type=”checkbox” name=”terms” value=”yes”/></form>


$value = $_POST[‘terms’];

echo $value;


In the above code we’ve given the input tag a name=”terms” and you have to target that in PHP using the $_POST global array to get the value, but make sure to add the value=”anything” attribute which is necessary for an <input> having checkbox attribute, whatever you’ve given in the value attribute, that will be taken by $_POST array and we’ve passed that to a local variable $value. So the above code will display “yes” on the page.

How to get values from multiple checkboxes?

Now here it comes another part when you have multiple checkboxes for users and you want to get values from all of them at once, so that you can store it in the database. The following code will get all the values if multiple checkboxes are selected by the user:

<form action=”page.php” method=”post”><input type=”checkbox” name=”courses[]” value=”photoshop”/>

<input type=”checkbox” name=”courses[]” value=”php”/>

<input type=”checkbox” name=”courses[]” value=”html”/>

<input type=”checkbox” name=”courses[]” value=”javascript”/>



foreach($_POST[‘courses’] as $values)
echo $values;

in the above example, we first added [] square brackets to the name “courses” that’s necessary when you want to get multiple values coming through checkboxes, this makes the values as an Array which we can easily get using the foreach loop in PHP, in the above example, if all four checkboxes are selected by the user, the page will display all the names of the courses we have given in the value attribute.

How to get values from the radio buttons?

This is quite similar procedure as checkboxes, because for both you have to add the same names but different types, if we want to get value of a radio button in PHP then we need to write a code something like this:

<form action=”page.php” method=”post”><input type=”radio” name=”select” value=”banana”/>



$value = $_POST[‘select’];

echo $value;


The above example is the same as we did in the checkbox part except the type has been changed from checkbox to radio. You can get multiple values from the radio buttons using the same syntax as we did for getting multiple values from checkboxes.

If you have any questions then let me know by commenting on this post.

About Abdul Wali

Abdul Wali is a professional blogger, marketer and web developer working online for last 5 years, he's been deeply involved in PHP, Web Development, SEO and computer video tutorials in different languages i.e English, Urdu and Pashto. You can catch him on and add him on Facebook.

Check Also

web hosting

The Best Web Hosting Providers (Tutorial)

Web hosting is one of the important aspects for establishing a business or website online. If ...

  • akram

    thanks a lot for your benevolent and great tutorials i hope to get a clue behind how to make pdf form filled from a form php .actually i see in youtube that there are a libraries that convert form html to pdf but really i still dont know how to grasp these tricks.

  • umer

    sir i have one question when we click the checkbox, and submited the data and data inserted in the database and display on page now we want to update the record and we click on the update button. then i didnot see which checkbox i click . so i want to know what we do for this that i see the checkbox click when we click on update button.

  • monalisa

    trying to figure out how to send multiple checkboxes to the database and it doesnt seem to work, im using another tutorial to add them to a html table ive tried everything. it seems to work with just two but not anymore than that.