I wanted to check which radio button is checked. Then, I looked at the questions in here before i asked this question and they said that the code
if(document.getElementById('number1').checked)
is the answer. But, i got the error "Use of undefined constant document - assumed 'document'" and
Call to undefined function getElementById().
Where did it go wrong? Did i have to write the function of getElementById('number1').checked
because it says "undefined"?
Thanks
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.
To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.
Your code is Javascript. To check the value of a radio button in PHP, it needs to have a name
attribute, which was sent in a form either by a GET or POST.
// If form method='get'
if (isset($_GET['name_of_radio_group'])) {
// Show the radio button value, i.e. which one was checked when the form was sent
echo $_GET['name_of_radio_group'];
}
// If form method='post'
if (isset($_POST['name_of_radio_group'])) {
// Show the radio button value, i.e. which one was checked when the form was sent
echo $_POST['name_of_radio_group'];
}
The code you have posted is in JavaScript. In order to determine is to submit a form as a post or get and query the value with the superglobals $_POST[], $_GET[], $_REQUEST[].
You have your HTML code:
<input type="radio" name="radio_group1" value="rg1v1" />Radio Group 1 - Value 1<br />
<input type="radio" name="radio_group1" value="rg1v2" />Radio Group 1 - Value 2<br />
<input type="radio" name="radio_group1" value="rg1v3" />Radio Group 1 - Value 3<br />
Assuming that you submitted the form using the post method to your php file the following code will test for which radio button is selected.
<?php
switch($_POST['radio_group1']) {
case "rg1v1":
$value = "Radio Group 1 - Value 1 has been selected.";
break;
case "rg1v2":
$value = "Radio Group 1 - Value 2 has been selected.";
break;
case "rg1v3":
$value = "Radio Group 1 - Value 3 has been selected.";
break;
default:
$value = "No radio has been selected for Radio Group 1";
}
?>
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