Home » Php » php – Radio button checked property

php – Radio button checked property

Posted by: admin July 12, 2020 Leave a comment


How can I know if the radio button checked property is true in PHP, can anyone give me an example?


How to&Answers:

You can try to ask for the checked attribute, see the example:

<label for="public0"><input type="radio" checked="checked" name="publicar" id="public0" value="TRUE" /> YES</label>
<label for="public1"><input type="radio" name="publicar" id="public1" value="FALSE" /> NO</label>

Then get the value of the raddio button in php: $publicar=$postvars[‘publicar’]; and ask for its value in order to know if it is TRUE or FALSE

In addition, if you want to manipulate the values using javascript:

if ( $("public0").checked == true) 
{ ...} or if ( $("public1").checked == true){...}
 //alert($("public0").checked); //if you want to see the value

Note: $postvars=$_POST


After submitting your form, in $_POST (or $_GET respectively) you’ll have a key with your radio button name and value with your radio button value, if it was checked. Otherwise, there will be no such key at all.

So <input type="radio" name="test" value="checked!" checked="checked" />
Will produce $_POST['test'] == 'checked!'


If your checkbox is checked, it is represented by a key=>value pair in your $_POST or $_GET array. So, if you want a boolean to know if it’s checked or not use this:

$checked = (isset($_POST['checkbox_name']))?true:false;

If you want the actual value of the checkbox:

$checked = (isset($_POST['checkbox_name']))?$_POST['checkbox_name']:NULL;

Replace $_POST with $_GET, depending on the method of your form.


It depends of your radio button name
For example, if you got 2 radio buttons with name = 'gender' and you checked the one with value='male', you’ll have in PHP $_POST['gender'] equals to male (or $_GET with GET)