0

所以这是一个基本的表单提交和处理,很简单。共有三个脚本,表单脚本、处理器脚本和视图页面。以下是每个片段的片段:

形式:

      <form action="processor.php" method="post">
      <h4>Question # 1</h4>
      <p>What grade are you in?</p>
      <label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
      <label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
      <label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
      <label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
      <div class="button">
      <button class="btn btn-primary" input type="submit" name="submit" href="processor.php">Submit</button>
      </div>
      </form>

处理器脚本:

 <?php

 header("Location: viewpage.html");

 $grade = $_POST['grade'];

 function Grades () {
 if ($grade =="1") {
    echo "You're a freshmen";
} elseif ($grade == "2") {
    echo "You're a sophomore";
} elseif ($grade == "3") {
    echo "You're a junior.";
} elseif ($grade == "4") {
    echo "You're a senior.";
} else {
    echo "Something is wrong.";
}

}

?>

查看页面:

      <h4>Question # 1</h4>
      <p><?php Grades();
      ?>
      </p>

本质上,我试图让用户填写表格,处理它并吐出输入。例如,在他们说自己是新生的表格上,他们单击提交并被带到显示“你是新生”的查看页面。自从我编码以来已经有一段时间了,这是一个非常简单的问题,我似乎无法找到解决方案。任何帮助将不胜感激。谢谢你。

照原样,当我进入查看页面时,它什么也没有显示。

4

5 回答 5

2

第一个问题是您在处理器脚本中做的第一件事是发出标头重定向。之后脚本可能会生成输出,但客户端永远不会看到它。第二个是你的程序结构全部关闭。理想情况下,您希望使用会话来存储处理器和视图脚本之间的“等级”值,或者将它们合并为一个。

<?php
$grade = $_POST['grade'];

function Grades ($input) {
if ($input =="1") {
    echo "You're a freshmen";
} elseif ($input == "2") {
    echo "You're a sophomore";
} elseif ($input == "3") {
    echo "You're a junior.";
} elseif ($input == "4") {
    echo "You're a senior.";
} else {
    echo "Something is wrong.";
}

?><html>
<head></head>
<body>
<h4>Question # 1</h4>
<p><?php Grades($grade);?></p>
</body>
</html>

或者,或者:

<?php
session_start();
$_SESSION['grade'] = $_POST['grade'];
header('Location: viewpage.php');

<?php
session_start();
$grade = $_SESSION['grade'];

function Grades ($input) {
if ($input =="1") {
    echo "You're a freshmen";
} elseif ($input == "2") {
    echo "You're a sophomore";
} elseif ($input == "3") {
    echo "You're a junior.";
} elseif ($input == "4") {
    echo "You're a senior.";
} else {
    echo "Something is wrong.";
}

?><html>
<head></head>
<body>
<h4>Question # 1</h4>
<p><?php Grades($grade);?></p>
</body>
</html>

您还需要记住的是,除非您将include()一个脚本转换成另一个脚本,否则每个脚本都完全忽略了其他脚本。他们不知道定义了哪些变量或函数,会话变量除外。

于 2013-04-05T15:13:05.507 回答
1

在我看来,您正在接收帖子,然后立即重定向到另一个页面,而不发送您刚刚收到的任何变量。因此,您需要将grade字段作为 GET 参数发送。您需要在视图文件中定义您的方法/变量。

处理器:

<?php

  // If you're going to redirect, you MUST send along the grade
  header("Location: viewpage.html?grade=" urlencode($_POST['grade'));

?>

看法:

<?php

  // In your view you only have access to variables in your immediate request.
  $grade = $_GET['grade'];

  function Grades ($grade) {
    if ($grade =="1") {
      echo "You're a freshmen";
    } elseif ($grade == "2") {
      echo "You're a sophomore";
    } elseif ($grade == "3") {
      echo "You're a junior.";
    } elseif ($grade == "4") {
      echo "You're a senior.";
    } else {
      echo "Something is wrong.";
    }
  }
?>

<h4>Question # 1</h4>
<p><?php Grades($grade); ?></p>

除非你真的需要处理器来保存数据:

  <form action="viewpage.html" method="get">
  <h4>Question # 1</h4>
  <p>What grade are you in?</p>
  <label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
  <label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
  <label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
  <label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
  <div class="button">
  <button class="btn btn-primary" type="submit" name="submit">Submit</button>
  </div>
  </form>
于 2013-04-05T15:09:07.843 回答
1

它应该看起来像这样:

<?php

 function Grades () {
 $grade = $_POST['grade'];
 if ($grade =="1") {
    echo "You're a freshmen";
} elseif ($grade == "2") {
    echo "You're a sophomore";
} elseif ($grade == "3") {
    echo "You're a junior.";
} elseif ($grade == "4") {
    echo "You're a senior.";
} else {
    echo "Something is wrong.";
}

}

include('viewpage.php');

?>

请注意,您应该将 viewpage.html 重命名为 viewpage.php

于 2013-04-05T15:10:13.773 回答
0

您可以使用一些功能来重定向不同的输入,例如更新鲜...为此,您必须创建单独的页面并使用 redirect("Pagename"); 我认为这可能是您问题的解决方案

于 2013-04-05T15:09:03.487 回答
0

您需要将POST变量作为参数传递给您的函数。

于 2013-04-05T15:09:41.443 回答