php 哪个单选按钮被选中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7086846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:59:01  来源:igfitidea点击:

which radio button is checked?

phphtml

提问by user893970

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').checkedbecause it says "undefined"? Thanks

哪里出错了?我是否必须编写函数,getElementById('number1').checked因为它说“未定义”?谢谢

回答by Michael Berkowski

Your code is Javascript. To check the value of a radio button in PHP, it needs to have a nameattribute, which was sent in a form either by a GET or POST.

您的代码是 Javascript。要在 PHP 中检查单选按钮的值,它需要有一个name属性,该属性通过 GET 或 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'];
}

回答by Steve Buzonas

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[].

您发布的代码是用 JavaScript 编写的。为了确定是将表单作为帖子提交还是使用超全局变量 $_POST[]、$_GET[]、$_REQUEST[] 获取和查询值。

You have your HTML code:

你有你的 HTML 代码:

<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.

假设您使用 post 方法将表单提交到您的 php 文件,以下代码将测试选择了哪个单选按钮。

<?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";
    }
?>

回答by Coomie

Where do you want to know if the radio button is checked? In the clients browser? Or on the server?

您想在哪里知道单选按钮是否被选中?在客户端浏览器中?还是在服务器上?

If you want to check on the client, you use javascript's

如果要检查客户端,请使用 javascript

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

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

如果要检查服务器,请使用 Michael 的 PHP