Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which radio button is checked?

Tags:

html

php

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

like image 919
user893970 Avatar asked Aug 17 '11 00:08

user893970


People also ask

How do you know radio button is checked or not?

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.

How can I know which radio was clicked or selected?

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.


2 Answers

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'];
}
like image 101
Michael Berkowski Avatar answered Sep 30 '22 20:09

Michael Berkowski


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";
    }
?>
like image 44
Steve Buzonas Avatar answered Sep 30 '22 21:09

Steve Buzonas