检查是否使用POST检查了radiobutton

时间:2012-11-21 00:51:21

标签: php html forms radio-button

我试图将用户重定向到另一个网页,具体取决于他们检查过哪个单选按钮。

以下是相关代码:

  <form action="sample.php" method="post">
          <input name="survey" type="radio" value="Yes" /> Yes
          </br>
          <input name="survey" type="radio" value="No"  /> No
  </form>

  <? 
    if ($_POST['survey'] == "Yes")
    {
        header('Location: http://localhost/survey.php');
    }
    else if ($_POST['survey'] == "No")
    {
        header('Location: http://localhost/survey.php');
    }

  ?>

由于某种原因,我的if语句中出现错误。这不承认&#39;调查&#39;作为有效的索引。我怎么没有把我的表单链接到php代码?

5 个答案:

答案 0 :(得分:2)

您的警告是由于当您使用GET(正常请求)加载页面时未设置$_POST['survey']

您可以通过在每次使用前添加isset($_POST['survey'] ) &&来更改条件,也可以将整个代码放在一个块中,以检查帖子是否如下所示:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    if ($_POST['survey'] == "Yes")
    {
        header('Location: http://localhost/survey.php');
    }
    else if ($_POST['survey'] == "No")
    {
        header('Location: http://localhost/survey.php');
    }
}
else
{
  // output html
}

无论哪种方式,你都必须把它放在你的html前面,因为如果标题已经被发送(东西已经被输出到浏览器)你就不能使用header

答案 1 :(得分:0)

考虑表单的工作原理:

首次访问您的网页时,表单已提交。然而,你的if/else表现得好像一样。这就是导致错误的原因 - $_POST['survey']第一次不存在。

正确编写脚本 - 在呈现HTML之前执行所有潜在表单处理:

<?php
if (isset($_POST['submit'])) {
    // handle the form
}
?>

<!doctype html>
<html>
    <head>
        <title>Blah</title>
    </head>

    <body>
        <!-- code -->
    </body>
</html>

这将允许您检查您是否已将表单提交给自己,并可能使用header()重定向用户,而不会遇到那些讨厌的“已发送标头”错误。

答案 2 :(得分:0)

尝试使用简单的print_r()语句来查看$ _POST是否包含任何内容。把它放在页面顶部:

print_r($_POST);

另外,请确保您通过表单加载结果页面。如果您只是键入页面的URL,它将不会随之发送任何POST数据。

答案 3 :(得分:0)

第一次加载文件sample.php时没有POST数据,因此没有索引“调查”。

您需要将其嵌套在另一个if语句中,或者修改如下:

  <form action="sample.php" method="post">
          <input name="survey" type="radio" value="Yes" /> Yes
          </br>
          <input name="survey" type="radio" value="No"  /> No
  </form>

  <? 
    if (isset($_POST) && $_POST['survey'] == "Yes")
    {
        header('Location: http://localhost/survey.php');
    }
    else if (isset($_POST) && $_POST['survey'] == "No")
    {
        header('Location: http://localhost/survey.php');
    }

  ?>

答案 4 :(得分:0)

我使用不同的php文件进行检查。

<html>
<body>
<form action="another.php" method="POST">
  <label>You are: </label> Male <input type="radio" name="male">  Female     <input type="radio" name="female"><br/><br/>
    <input type="submit" name="submit" value="GO">
</body>
</html>

***** another.php ******

<?php
if (isset($_POST['submit'])) 
{
    if(empty($_POST['male']) && empty($_POST['female'])) 
    {echo "select gender";}
    elseif (empty($_POST['female'])) //means Male is checked
     {
        $gend="Male";
          echo $gend;
    }
    elseif(empty($_POST['male'])) //Means Female is checked
    { 
       $gend="Female";
          echo $gend; 
    }
    elseif(empty($_POST['male']) || empty($_POST['female'])== false) //Not required if you can disable one when another is checked
        {echo"please select only one";}
}
?>
相关问题