如何在 PHP 中发送单选按钮值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14747803/
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
How to send radio button value in PHP
提问by René Hansen
I am struggling with sending the value of a radiobutton to an email.
我正在努力将单选按钮的值发送到电子邮件。
I have coded 2 radiobuttons, where I have set the first on to be default checked.
我编写了 2 个单选按钮,其中我将第一个设置为默认选中。
The form and values work, however the radio button value is not submitted.
表单和值有效,但未提交单选按钮值。
Any wise words?
有什么好话吗?
回答by ajmal
When you select a radio button and click on a submit button, you need to handle the submission of any selected values in your php code using $_POST[]For example:
if your radio button is:
当您选择一个单选按钮并单击一个提交按钮时,您需要使用$_POST[]例如:如果您的单选按钮是:
<input type="radio" name="rdb" value="male"/>
then in your php code you need to use:
然后在你的 php 代码中你需要使用:
$rdb_value = $_POST['rdb'];
回答by Kits
Check whether you have put name="your_radio" where you have inserted radio tag
检查您是否将 name="your_radio" 放在插入单选标签的位置
if you have done this then check your php code. Use isset()
如果您已经这样做了,请检查您的 php 代码。使用 isset()
e.g.
例如
if(isset($_POST['submit']))
{
/*other variables*/
$radio_value = $_POST["your_radio"];
}
If you have done this as well then we need to look through your codes
如果您也这样做了,那么我们需要查看您的代码
回答by phpalix
The radio buttons are sent on form submit when they are checked only...
单选按钮仅在选中时在表单提交时发送...
use isset()if true then its checked otherwise its not
isset()如果为真则使用它,否则它不会被检查
回答by Abhijeet Vaikar
When you select a radio button and click on a submit button, you need to handle the submission of any selected values in your php code using $_POST[]
For example:
if your radio button is:
当您选择一个单选按钮并单击一个提交按钮时,您需要使用 $_POST[] 处理您的 php 代码中任何选定值的提交,
例如:
如果您的单选按钮是:
<input type="radio" name="rdb" value="male"/>
then in your php code you need to use:
然后在你的 php 代码中你需要使用:
$rdb_value = $_POST['rdb'];
回答by laxonline
Radio buttons have another attribute - checked or unchecked. You need to set which button was selected by the user, so you have to write PHP code inside the HTML with these values - checked or unchecked. Here's one way to do it:
单选按钮有另一个属性 - 选中或未选中。您需要设置用户选择了哪个按钮,因此您必须使用这些值在 HTML 中编写 PHP 代码 - 选中或未选中。这是一种方法:
The PHP code:
PHP代码:
<?PHP
$male_status = 'unchecked';
$female_status = 'unchecked';
if (isset($_POST['Submit1'])) {
$selected_radio = $_POST['gender'];
if ($selected_radio == 'male') {
$male_status = 'checked';
}else if ($selected_radio == 'female') {
$female_status = 'checked';
}
}
?>
The HTML FORM code:
HTML 表单代码:
<FORM name ="form1" method ="post" action ="radioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male'
<?PHP print $male_status; ?>
>Male
<Input type = 'Radio' Name ='gender' value= 'female'
<?PHP print $female_status; ?>
>Female
<P>
<Input type = "Submit" Name = "Submit1" VALUE = "Select a Radio Button">
</FORM>
回答by Devang Rathod
Should be :
应该 :
HTML :
HTML :
<form method="post" action="">
<input id="name" name="name" type="text" size="40"/>
<input type="radio" name="radio" value="test"/>Test
<input type="submit" name="submit" value="submit"/>
</form>
PHP Code :
PHP代码:
if(isset($_POST['submit']))
{
echo $radio_value = $_POST["radio"];
}

