如何在 PHP 中获取单选按钮的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29542576/
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 do I get the value of a radio button in PHP?
提问by
I've created a basic website that requires the user to select a radio button. I want a PHP file to retrieve the value of the radio button that was chosen and respond accordingly, but the file does not currently produce any output. What is wrong with the code I am using now? Why can my PHP file not retrieve the radio button value properly?
我创建了一个需要用户选择单选按钮的基本网站。我想要一个 PHP 文件来检索选择的单选按钮的值并做出相应的响应,但该文件目前不产生任何输出。我现在使用的代码有什么问题?为什么我的 PHP 文件不能正确检索单选按钮值?
Index.html:
索引.html:
<form method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
</form>
<form method="GET" action="Result.php">
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
Result.php:
结果.php:
<?php
$radioVal = $_POST["MyRadio"];
if($radioVal == "First")
{
echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
echo("Second, eh?");
}
?>
采纳答案by tworabbits
Your are using two separate forms for your general input elements and one consisting of a submit button only.
您正在为您的一般输入元素使用两个单独的表单,一个仅包含一个提交按钮。
Include the submit button in the first form and it should work fine:
在第一个表单中包含提交按钮,它应该可以正常工作:
<form method="POST" action="Result.php">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
回答by Peeyoosh Kumar Gupta
<form method="post">
<input type="radio" name="MyRadio" value="First" checked>First<br> <!--This one is automatically checked when the user opens the page-->
<input type="radio" name="MyRadio" value="Second">Second
</br>
<input type="submit" value="Result" name="Result"> <!--This button opens Result.php-->
</form >
In my php code you can see that the function of isset()
that set that when your PHP code run. In your code you mention $radioVal = $_POST["MyRadio"];
where MyRadio is undefined index for PHP. Here when we submit the form then submit call the PHP code without any lag and you also use the double form. This is wrong for this code.
在我的 php 代码中,您可以看到isset()
该设置的函数在您的 PHP 代码运行时设置。在您的代码中,您提到$radioVal = $_POST["MyRadio"];
MyRadio 是 PHP 的未定义索引。在这里,当我们提交表单然后提交时,没有任何延迟地调用 PHP 代码,您也使用双重表单。这段代码是错误的。
<?php
if (isset($_POST['Result']))
{
$radioVal = $_POST["MyRadio"];
if($radioVal == "First")
{
echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
echo("Second, eh?");
}
}
?>
回答by Amit Mhaske
First of all you are doing it a little wrong. You are using two forms to do the task. Let me tell you how can you do it.
首先,你做的有点不对。您正在使用两种形式来完成任务。让我告诉你你怎么能做到。
index.html
索引.html
<form action= "result.php" method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> <!--This one is automatically checked when the user opens the page -->
<input type="radio" name="MyRadio" value="Second">Second
<br/>
<input type="submit" value="Result" name="Result"> <!--//This button opens Result.php -->
result.php
结果.php
<?php
echo $_POST["MyRadio];
// on new page you will get "First" or "Second", depending on what you have selected on html page
?>
回答by Dallin
This is the form that's being submitted. It has an action attribute which directs it to Result.php.
这是提交的表格。它有一个指向 Result.php 的 action 属性。
<form method="GET" action="Result.php">
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
In order for you to get the data you want in Results.php, you need to add the radio buttons to this form
为了让你在 Results.php 中得到你想要的数据,你需要在这个表单中添加单选按钮
<form method="POST" action="Result.php">
<input type="radio" name="MyRadio" value="First" checked>First<br>
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result">
</form>
You're also going to need to change your method to POST if you're going to use the $_POST superglobal
如果您要使用 $_POST 超全局变量,您还需要将方法更改为 POST
$radioVal = $_POST["MyRadio"];
回答by Rishi
You are using two separate forms for the html code, which means the first form is actually not submitted when you press the button.
您正在为 html 代码使用两个单独的表单,这意味着当您按下按钮时,第一个表单实际上并未提交。
You shouldn't need to change the PHP code in result.php
, but you should ideally use one form.
您不需要更改 中的 PHP 代码result.php
,但最好使用一种形式。
<form method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>