user893970 user893970 - 6 months ago 8
HTML Question

which radio button is checked?

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

Answer

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'];
}