【问题标题】:How do I get the value of a radio button in PHP?如何在 PHP 中获取单选按钮的值?
【发布时间】:2015-06-15 01:25:45
【问题描述】:

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

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

结果.php:

<?php
$radioVal = $_POST["MyRadio"];

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

【问题讨论】:

    标签: php html


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:
      <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?");
      }
      }
      ?>
      

      【讨论】:

        【解决方案3】:

        这是正在提交的表单。它有一个 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"];
        

        【讨论】:

          【解决方案4】:

          首先你做的有点不对。您正在使用两种形式来完成任务。让我告诉你怎么做。

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

          结果.php

              <?php 
                  echo $_POST["MyRadio];
                  // on new page you will get "First" or "Second", depending on what you have selected on html page
          ?>
          

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              猜你喜欢
              • 2018-05-04
              • 1970-01-01
              • 1970-01-01
              • 2018-10-07
              • 2011-01-09
              • 1970-01-01
              • 2021-12-29
              相关资源
              最近更新 更多