I'm using php. I'd like to know how can I test if a radio button is selected and get the value? i can test if the radio button is selected but i cannot get the value. I created a button to test this in my form. First I select a radio button, then i click on the button and it must display a message that says which value i selected and put this value into a variable. In order to test if a radio button is selected i did like this:
$selected_radio=$_POST['SINGLE_' . $question->id . $multi_name_adjust . ''];
if ($selected_radio = 'checked'){}
Thanks
To get selected value of a radio button: php if (isset($_POST['submit'])) { if(isset($_POST['radio'])) { echo "You have selected :". $_POST['radio']; // Displaying Selected Value } ?>
Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.
It has been many years since I even looked at any PHP but if I recall correctly, you can just check $_POST["NAME_OF_RADIOBUTTON"]. if the radio button is selected, the value should be present, if it's not selected, the value in $_POST["NAME_OF_RADIOBUTTON"] will be null.
It's pretty simple, take a look at the code below:
The form:
<form action="result.php" method="post">
Answer 1 <input type="radio" name="ans" value="ans1" /><br />
Answer 2 <input type="radio" name="ans" value="ans2" /><br />
Answer 3 <input type="radio" name="ans" value="ans3" /><br />
Answer 4 <input type="radio" name="ans" value="ans4" /><br />
<input type="submit" value="submit" />
</form>
PHP code:
<?php
$answer = $_POST['ans'];
if ($answer == "ans1") {
echo 'Correct';
}
else {
echo 'Incorrect';
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With