Which switch is installed?

I wanted to check which switch is set. Then I looked at the questions here before I asked this question, and they said the code

if(document.getElementById('number1').checked) 

- the answer. But, I got the error "Using an undefined persistent document - the alleged" document "and

 Call to undefined function getElementById(). 

Where did this happen? I need to write the getElementById('number1').checked function because it says "undefined"? Thanks

+4
source share
3 answers

Your code is Javascript. To test the value of a switch in PHP, it must have a name attribute that was submitted in the form of either GET or POST.

 // If form method='get' if (isset($_GET['name_of_radio_group'])) { // Show the radio button value, ie 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, ie which one was checked when the form was sent echo $_POST['name_of_radio_group']; } 
+6
source

The code you posted is in JavaScript. To determine whether to send the form as a message or to receive and request a value with superglobal $ _POST [], $ _GET [], $ _REQUEST [].

You have 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 you submitted the form using the post method to your php file, the following code will check 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"; } ?> 
+2
source

Where do you want to know if the switch is on? In the client browser? Or on the server?

If you want to check on the client, you use

javascript
 if (document.getElementById('number1').checked) 

If you want to check on the server, you use Michael PHP

+1
source

All Articles