【问题标题】:how to redirect in condition with php如何使用php在条件下重定向
【发布时间】:2014-09-30 11:18:48
【问题描述】:

我对 php 中的重定向有疑问
我有 2 页
- 索引首页
- 第二页用于进程编辑或edit.php

我的问题是..
如果我点击编辑,他肯定在 edit.php 中,如果我点击保存在 edit.php
如果我的重定向代码
header('location:index.php');

如何返回或重定向到索引但仍处于逻辑 2 或
$_POST['select']=='2'

这是我的代码:

<form action='' methode='post'>
  <select name='select'>
  <option value='1'>example1</option>
  <option value='2'>example2</option>
  </select>
</form>
<?php
if($_POST['select']=='1')
{
echo"<a href='?page=edit&id=$r[id]'>Edit</a>";
}
elseif($_POST['select']=='2')
{
echo"<a href='?page=edit&id=$r[id]'>Edit</a>";
}
else
{
echo"please select";
}
?>

【问题讨论】:

  • 您需要在两个$r[id] 周围加上大括号({ })。除此之外,我不知道你在问什么。
  • hy worldofir 如果我的代码不完整,我很抱歉,因为它只是举例,但我的问题是更多的重定向,如 header('location:index.php');但是如何定向到索引但仍处于状态 $_POST['select']=='2'

标签: php html mysql css if-statement


【解决方案1】:

标题,如果你有它们,必须是第一个输出到浏览器的东西。这包括在打开 &lt;?php 标记之前没有任何空格。

如果您想对要发送的标头进行比较,那么您可以,但这应该是您在获取 $_POST 变量进行比较之后要做的第一件事。

另外,您需要在 Location 标头中使用绝对地址。

例如;

<?php
if($_POST['select']=='1')
{
    header('Location: http://yoursite.com/index.php');
}
elseif($_POST['select']=='2')
{
echo"<a href='?page=edit&id={$r[id]}'>Edit</a>";
}
else
{
echo"please select";
}
?>
<form action='edit.php' method='post'>
  <select name='select'>
  <option value='1'>example1</option>
  <option value='2'>example2</option>
  </select>
</form>

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2023-03-30
    • 2014-06-17
    • 2012-09-17
    • 2015-12-27
    相关资源
    最近更新 更多