php 单选按钮选中属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3976534/
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
Radio button checked property
提问by aintgel
How can I know if the radio button checked property is true in PHP, can anyone give me an example?
我怎么知道 PHP 中的单选按钮检查属性是否为真,谁能给我一个例子?
Thanks..
谢谢..
采纳答案by Nervo Verdezoto
You can try to ask for the checked attribute, see the example:
你可以尝试询问checked属性,看例子:
<label for="public0"><input type="radio" checked="checked" name="publicar" id="public0" value="TRUE" /> YES</label>
<label for="public1"><input type="radio" name="publicar" id="public1" value="FALSE" /> NO</label>
Then get the value of the raddio button in php: $publicar=$postvars['publicar'];and ask for its value in order to know if it is TRUE or FALSE
然后在php中获取radio按钮的值:$publicar=$postvars['publicar']; 并询问它的值以了解它是 TRUE 还是 FALSE
In addition, if you want to manipulate the values using javascript:
此外,如果您想使用 javascript 操作这些值:
if ( $("public0").checked == true)
{ ...} or if ( $("public1").checked == true){...}
//alert($("public0").checked); //if you want to see the value
//alert($("public1").checked);
Note: $postvars=$_POST
注意:$postvars=$_POST
回答by NOtherDev
After submitting your form, in $_POST (or $_GET respectively) you'll have a key with your radio button name and value with your radio button value, if it was checked. Otherwise, there will be no such key at all.
提交表单后,在 $_POST(或分别为 $_GET)中,如果选中,您将拥有一个带有单选按钮名称的键和带有单选按钮值的值。否则,根本就没有这样的钥匙。
So <input type="radio" name="test" value="checked!" checked="checked" />
Will produce $_POST['test'] == 'checked!'
所以<input type="radio" name="test" value="checked!" checked="checked" />
会产生$_POST['test'] == 'checked!'
回答by thomaux
If your checkbox is checked, it is represented by a key=>value
pair in your $_POST or $_GET array. So, if you want a boolean to know if it's checked or not use this:
如果您的复选框被选中,则它由key=>value
$_POST 或 $_GET 数组中的一对表示。所以,如果你想让一个布尔值知道它是否被检查过,请使用这个:
$checked = (isset($_POST['checkbox_name']))?true:false;
If you want the actual value of the checkbox:
如果您想要复选框的实际值:
$checked = (isset($_POST['checkbox_name']))?$_POST['checkbox_name']:NULL;
Replace $_POST with $_GET, depending on the method of your form.
将 $_POST 替换为 $_GET,具体取决于表单的方法。
回答by Shikiryu
It depends of your radio button name
For example, if you got 2 radio buttons with name = 'gender'
and you checked the one with value='male'
, you'll have in PHP $_POST['gender']
equals to male
(or $_GET with GET)
这取决于你的单选按钮name
。例如,如果你有2个单选按钮name = 'gender'
,你检查了一个有value='male'
,你就必须在PHP$_POST['gender']
等于male
(或$ _GET用GET)