【问题标题】:Header redirects to the same page, not pages specified标头重定向到同一页面,而不是指定的页面
【发布时间】:2014-05-21 18:02:32
【问题描述】:

我已经阅读了这个网站上的很多答案,但是所有的解决方案都没有解决这个问题。我有代码可以根据他们为调查项目提交的表单数据使用 PHP 重定向页面。标题应根据下拉列表中的用户选择重定向到另一个页面。所有这些都存储在我的本地 WAMP 服务器上的 www 目录中。问题是,页面不会重定向到我在标头重定向中指定的页面,而是重新加载当前页面。我需要它重定向到标题中指定的页面。

这里是 PHP:

    <?php
    session_start();

    if(isset($_POST['post'])) {
    $_SESSION['maintasked'] = $_POST['maintask'];
    $_SESSION['dated'] = $_POST['date'];
    $_SESSION['named'] = $_POST['name'];
    session_write_close();

    if(!empty($_POST['name']))
    {
    switch($_POST['maintask']) {

    case "Pre Cabinet":
    header('Location: /Tier2PreCabinet.php');
    exit;
    break;

    case "At Cabinet":
    header('Location: /Tier2AtCabinet.php');
    exit;
    break;

    case "Post Cabinet":
    header('Location: /Tier2PostCabinet.php');
    exit;
    break;

    }
   }
  }
?>

这里是 HTML:

<!doctype html>
<html>
<head>
<meta charset = "utf-8">
<title>Advanced RV Time Records</title>
</head>
<body>

<h2>Advanced RV Time Capsule</h2>

<!-- This form grabs today's date from the JS run at page load and allows the user to change it if necessary -->
<p>Enter the date of the day the work was done.</p>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
        <input type="date" id="theDate" value="" name ="date">

<!-- This form has the user select the name they will be working under. Must be completed. -->  
<p id ="name" name="named">Enter the name of the worker performing the task. </p>

<select id="workers" name="name">
<option value =null></option>
<option>Brian</option>
<option>Mike G.</option>
<option>Anthony</option>
<option>Tom</option>
<option>Joe</option>
<option>Deury</option>
<option>Chris Puetzfeld</option>
<option>Chris Piercy</option>
<option>Frank</option>
</select>


<!-- This form has the user select the task they will be working on.-->
<p>Enter the main task that the worker will be doing</p>

<select id ="MainTask" name ="maintask">
<option id ="1">Pre Cabinet</option>
<option value ="2">At Cabinet</option>
<option value ="3">Post Cabinet</option>
</select>
<input type ="submit" value="Continue On">
</form>
</body>
</html>

【问题讨论】:

  • “你能找到错误”问题are not good questions for Stack Overflow。确保您提供一个简短但对问题的具体说明,准确地告诉我们哪里出了问题。 “它不起作用”不是问题陈述。
  • 抱歉,我尝试指定存在的问题。

标签: php html wamp


【解决方案1】:

如果设置,选择框会传递 value 属性中的值,而不是文本。在您的选择框中,您的第一个选项没有 value 属性,因此将提交文本。但是,在您的第二个和第三个选项中,您的值是 23,这就是将要提交的值。您的 php 代码正在寻找文本值,而不是将提交的数字值。

您可以将第一个选项上的id="1" 更改为value="1",然后在您的php 中查找值123。或者您可以从选项23 中删除value,文本应该提交。

【讨论】:

  • 我已经解决了这个问题,但是页面仍然会重定向到它自己。
  • 好吧,不如不进行实际的标头调用,而是回显它应该重定向到的位置。然后在开关上设置一个默认大小写为echo "Case not found: {$_POST['maintask']}";。这是非常标准的调试。如果有什么不好的地方,请尽可能多地回显。
  • 另外,您是否有可能正确地定向到另一个页面,但该页面由于某种原因被重定向回来?
  • 感谢您的回声建议,我对 PHP 和网页设计很陌生,所以我仍在学习。该页面没有通过 echo 输出任何内容,因此服务器或 PHP 配置本身一定有问题。谢谢!
  • 顺便说一句,标头重定向到的页面不包含任何重定向回标头所在页面的重定向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 2014-05-17
相关资源
最近更新 更多