Php 为什么重定向不起作用?
我用两个按钮创建了一个表-添加新行和删除行。删除它的代码称为WitAjax,并写入另一个php文件中。代码如下:Php 为什么重定向不起作用?,php,ajax,postgresql,Php,Ajax,Postgresql,我用两个按钮创建了一个表-添加新行和删除行。删除它的代码称为WitAjax,并写入另一个php文件中。代码如下: <?php ob_start(); //eliminates buffer collisions require_once('connect_db.php'); $name = $_POST['x']; $surname = $_POST['y']; $result = pg_query(connect(), "delete from lecturer where n
<?php
ob_start(); //eliminates buffer collisions
require_once('connect_db.php');
$name = $_POST['x'];
$surname = $_POST['y'];
$result = pg_query(connect(), "delete from lecturer where name='$name' and surname='$surname'");
//dump the result object
var_dump($result);
//reloading the page
header("location: lecturer.php?fail=2", TRUE,307);
?>
当我试图删除一行时,该行被删除,但我必须刷新页面才能看到这一点。“日志”窗口显示主页的内容,其中有“删除”按钮。
如果我用标题注释行;它显然没有重定向,日志窗口显示resource2的typepgsql结果。
有人知道我做错了什么吗
PS:当我使用“添加新行”按钮时,效果很好。它会立即显示新添加的行。这是插入新行的代码:
<?php
ob_start(); //eliminates buffer collisions
require_once('connect_db.php');
$id = time(); //creates a unique id using the unix time
$result = pg_query(connect(), "INSERT INTO lecturer VALUES ($id, '$_POST[name]','$_POST[surname]','$_POST[dep]')");
//dump the result object
var_dump($result);
//reloading the page
header("location: lecturer.php");
?>
好吧,我想得差不多了。 简而言之,我不必从php文件重定向。 我只是插入此函数以从ajax重定向:
.done(function( msg ) {
location.reload();
})
至于为什么必须这样做,我确实理解,但不能完全解释。
在找到解决方案谢谢,我会检查它,如果是这样的话,请删除我的问题。如果你只是在不设置307代码的情况下执行标题重定向并替换未设置的TRUE,是否有效?删除var_dump$result;删除项目后,为什么要使用状态代码307重定向?状态200通常表示成功,这在我看来更符合逻辑。如果您使用AJAX,那么从删除脚本返回true或false并使用javascript删除行(如果删除为true)是否更有意义让AJAX处理重新加载是更好的方法。我建议根据删除操作是否实际发生返回带true或false的json。
.done(function( msg ) {
location.reload();
})