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:
$value = $_POST[‘terms’];
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:
<input type=”checkbox” name=”courses” value=”php”/>
<input type=”checkbox” name=”courses” value=”html”/>
foreach($_POST[‘courses’] as $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:
$value = $_POST[‘select’];
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.