我创建了一个带有两个按钮的表格 - 添加新行和删除行。删除它的代码称为wit ajax,并编写在另一个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 name='$name' and surname='$surname'");
//dump the result object
var_dump($result);
//reloading the page
header("location: lecturer.php?fail=2", TRUE,307);
?>
我使用这个 ajax 函数来调用文件:
$.ajax({ type: "POST",
url: "delete_lecturer.php",
data: { x: names, y: surname}
})
当我尝试删除一行时,该行被删除,但我必须刷新页面才能看到这一点。日志窗口显示我有删除按钮的主页的内容。如果我用标题()注释该行;它不会重定向(显然),并且日志窗口显示"类型(PGSQL 结果)的资源(2)"。有谁知道我做错了什么?
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();
})
至于为什么必须这样做,我确实理解,但不能完全解释。在 https://www.udemy.com/blog/jquery-refresh-page/找到的解决方案