php 按钮提交后重定向到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15000591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:21:17  来源:igfitidea点击:

Redirect to another page after button submit

phpredirect

提问by Sindu

I am building an application where details are displayed (admin.php).The user clicks on edit(edit.php) and he is redirected to another page to edit his details. Once the edit is done the values are updated and I want to redirect to the previous page once again where the details are displayed. All the operations carry out as desired , but when I try to redirect inside the "submit" to the previous page it doesn't occur.

我正在构建一个显示详细信息的应用程序(admin.php)。用户点击编辑(edit.php),然后他被重定向到另一个页面来编辑他的详细信息。编辑完成后,值会更新,我想再次重定向到显示详细信息的上一页。所有操作都按需要执行,但是当我尝试将“提交”内部重定向到上一页时,它不会发生。

Admin.php

管理文件

    $event_id = $_SESSION['fedit'];
    if($_GET['edit'])
    {
     $url = "edit.php?event_id=". $event_id;  
     header("Location: $url");
    }

Edit.php

编辑.php

     if($_GET["Submit"])
              {
                 $val=$_GET['event'];
                 $remail=$_GET['email'];
                $rname = $_GET['ename'];
                $sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
                $page= "admin.php";
                header("Location: $page");
             }

回答by Sean Mishra

I don't know where you have placed this code on the page but if somehow a http header output is being sent before that header(location:"")then it wont work. Try the suggestions by other people, but if all else fails, you might wanna use JS.

我不知道您将此代码放在页面上的哪个位置,但是如果在该标头(位置:“”)之前以某种方式发送了 http 标头输出,那么它将无法工作。尝试其他人的建议,但如果所有其他方法都失败了,您可能想要使用 JS。

echo "<script>window.location.assign("admin.php")</script>";

Use that instead of

用它代替

header("Location: $page");

and it should work I guess.

我想它应该可以工作。

回答by Mithun Sen

add action='edit.php' method='get'to your form tag. and in edit.php page place your rest of the code after submit.

添加action='edit.php' method='get'到您的表单标签。并在 edit.php 页面中放置您提交后的其余代码。

回答by Joseph Callaars

Preferably do this:

最好这样做:

if ($_GET["Submit"]) {
   ...

   // Clean any output
   ob_clean();
   header("Location: $page");

   // Exit the PHP process 
   exit;
}

回答by Your Common Sense

It's very hard to tell what are you doing from the incomplete code you posted.
However, it looks like that not redirect but form submission is broken. There are also 2 serious flaws in your code:

很难从您发布的不完整代码中判断您在做什么。
但是,看起来不是重定向而是表单提交被破坏了。您的代码中还有 2 个严重缺陷:

  • Whatever edits have to be done using POST method, not GET
  • you have to properly format your SQL query.
  • 任何编辑都必须使用 POST 方法完成,而不是 GET
  • 您必须正确格式化您的 SQL 查询。

Anyway, here is an example of such an application (though editing only one field. you can easily add more) which works.

无论如何,这里有一个这样的应用程序示例(虽然只编辑一个字段。您可以轻松添加更多)。

<?  
mysql_connect(); 
mysql_select_db("new"); 
$table = "test"; 
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part: 
  $name = mysql_real_escape_string($_POST['name']); 
  if ($id = intval($_POST['id'])) { 
    $query="UPDATE $table SET name='$name' WHERE id=$id"; 
  } else { 
    $query="INSERT INTO $table SET name='$name'"; 
  } 
  mysql_query($query) or trigger_error(mysql_error()." in ".$query); 
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);  
  exit;  
}  
if (!isset($_GET['id'])) { //listing part: 
  $LIST=array(); 
  $query="SELECT * FROM $table";  
  $res=mysql_query($query); 
  while($row=mysql_fetch_assoc($res)) $LIST[]=$row; 
  include 'list.php'; 
} else { // form displaying part: 
  if ($id=intval($_GET['id'])) { 
    $query="SELECT * FROM $table WHERE id=$id";  
    $res=mysql_query($query); 
    $row=mysql_fetch_assoc($res); 
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v); 
  } else { 
    $row['name']=''; 
    $row['id']=0; 
  } 
  include 'form.php'; 
}  
?>

And you will need 2 templates too, to hold your HTML

你也需要 2 个模板来保存你的 HTML

form.php

表单.php

<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>

list.php

列表.php

<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>

回答by Husman

Actually, that is not going to work, isnce your edit.php will never reach its if statement, as you are not sending $_GET["submit"]. You need to do the following (send the submit variable by setting it to any value)

实际上,这是行不通的,因为您的 edit.php 永远不会到达它的 if 语句,因为您没有发送 $_GET["submit"]。您需要执行以下操作(通过将提交变量设置为任何值来发送它)

admin.php

管理文件

 $event_id = $_SESSION['fedit'];
 if($_GET['edit'])
 {
   $url = "edit.php?event_id=". $event_id . "&submit=1";
   header("Location: $url");
 }

edit.php

编辑.php

if($_GET["Submit"])
{
   $val=$_GET['event'];
   $remail=$_GET['email'];
   $rname = $_GET['ename'];
   $sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
   header("Location: admin.php");
}

回答by Suhel Meman

use header("Location : ". $page);instead of header("Location: $page");

使用 header("Location : ". $page);代替header("Location: $page");

 if($_GET["Submit"])
          {
             $val=$_GET['event'];
             $remail=$_GET['email'];
            $rname = $_GET['ename'];
            $sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
            $page= "admin.php";
            header("Location : ". $page);
         }