如何在PHP中获取单选按钮的值?

时间:2015-04-09 15:20:20

标签: php html

我创建了一个基本网站,要求用户选择一个单选按钮。我想要一个PHP文件来检索所选的单选按钮的值并相应地做出响应,但该文件当前不会产生任何输出。我现在使用的代码有什么问题?为什么我的PHP文件无法正确检索单选按钮值?

的index.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
$radioVal = $_POST["MyRadio"];

if($radioVal == "First")
{
    echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
    echo("Second, eh?");
}
?>

5 个答案:

答案 0 :(得分:11)

您使用两个单独的表单作为常规输入元素,另一个表单仅包含提交按钮。

在第一个表单中包含提交按钮,它应该可以正常工作:

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

答案 1 :(得分:3)

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

在我的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?");
}
}
?>

答案 2 :(得分:0)

这是提交的表单。它有一个action属性,可以将它定向到Result.php。

<form method="GET" action="Result.php">
    <input type="submit" value="Result" name="Result"> //This button opens Result.php
</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>

如果您要使用$ _POST超全球

,您还需要将方法更改为POST
$radioVal = $_POST["MyRadio"];

答案 3 :(得分:0)

首先,你做得有点不对劲。您正在使用两种表单来完成任务。我告诉你,你怎么能这样做。

的index.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 
        echo $_POST["MyRadio];
        // on new page you will get "First" or "Second", depending on what you have selected on html page
?>

答案 4 :(得分:0)

您正在为html代码使用两个单独的表单,这意味着当您按下按钮时,第一个表单实际上未提交。

您不需要更改result.php中的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>