为什么我的代码没有将用户重定向到所需的页面?
Why my code is not redirecting user to desired page?
下面的代码提交 post 并重定向到另一个页面。 Post 已提交,行在数据库中有效,但我无法在提交 post 后重定向。
error_reporting('E_ALL ^ E_NOTICE');
if(isset($_POST['submit'])) {
$title=strip_tags($_POST['title']);
$body=($_POST['body']);
$category=$_POST['category'];
if (empty($_POST['category'])) {
$er = "Please select a category from the options";
}
else if($category != "Controversies" && $category != "Entertainment" && $category != "Health" && $category != "Politics" && $category != "Lifestyle" && $category != "Technology" && $category != "Sports" && $category != "Travel"){
$er = "Please select a valid category";
}
else if (strlen($title) < 5) {
$er = "Make sure title is more than 5 characters";
}
else if (strlen($title) > 100 ) {
$er = "Make sure title is not more than 100 characters";
}
else {
$stmt = $db->prepare("INSERT INTO posts (status,userid, title, body,category) VALUES (:status,:userid,:title,:body,:category)");
$stmt->execute(array(':userid'=>$userid,':status'=>active,':title'=>$title,':body'=>$body,':category'=>$category));
if ($stmt->rowCount() > 0) {
header("Location: mains.php");
exit();
}
else {
$er = 'Some error occured please try again!';
}
}
}
如果我使用 $er = Post success
而不是 header("Location: mains.php");
我会在提交 post 后看到消息。那么这里的header有什么问题呢。为什么代码没有重定向到 mains.php
你需要像这样在 header 之后写死:
header("location: mains.php");
die();
尝试在代码顶部添加 ob_start();
,即在 error_reporting('E_ALL ^ E_NOTICE');
语句之前
首先回显$stmt->rowCount()
来检查它是否是returns一个值。如果 returns 值大于零,则在调用 header()
函数后添加 exit;
语句。
if ($stmt->rowCount() > 0) {
header("Location: mains.php");
exit;
}
else {
$er = "Some error occured please try again!";
}
// 编辑
我刚刚也注意到你 header 代码在 else 语句中,它可以
为真前的if语句引起的
您应该在 header 代码后加上 "die();"
所以 PHP 不会继续 运行 代码
并且可能会中断 header 的设置。
也请尝试使用绝对 URL 因为有时它会导致
PHP.
的问题
代码:
header("Location: http://www.website.com/mains.php");
die();
尝试使用 javascript location.href
,如下所示:
echo '<script>location.href = "example.com/mains.php";</script>';
而不是
header("Location: mains.php");
关于您的评论:
thanks it worked but any php solution ? javascript may be disabled by user
echo '<script>';
echo 'window.location.href="mains.php"'; //Javascript Redirect
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url=mains.php" />'; //Incase of Javascript disabled
echo '</noscript>';
下面的代码提交 post 并重定向到另一个页面。 Post 已提交,行在数据库中有效,但我无法在提交 post 后重定向。
error_reporting('E_ALL ^ E_NOTICE');
if(isset($_POST['submit'])) {
$title=strip_tags($_POST['title']);
$body=($_POST['body']);
$category=$_POST['category'];
if (empty($_POST['category'])) {
$er = "Please select a category from the options";
}
else if($category != "Controversies" && $category != "Entertainment" && $category != "Health" && $category != "Politics" && $category != "Lifestyle" && $category != "Technology" && $category != "Sports" && $category != "Travel"){
$er = "Please select a valid category";
}
else if (strlen($title) < 5) {
$er = "Make sure title is more than 5 characters";
}
else if (strlen($title) > 100 ) {
$er = "Make sure title is not more than 100 characters";
}
else {
$stmt = $db->prepare("INSERT INTO posts (status,userid, title, body,category) VALUES (:status,:userid,:title,:body,:category)");
$stmt->execute(array(':userid'=>$userid,':status'=>active,':title'=>$title,':body'=>$body,':category'=>$category));
if ($stmt->rowCount() > 0) {
header("Location: mains.php");
exit();
}
else {
$er = 'Some error occured please try again!';
}
}
}
如果我使用 $er = Post success
而不是 header("Location: mains.php");
我会在提交 post 后看到消息。那么这里的header有什么问题呢。为什么代码没有重定向到 mains.php
你需要像这样在 header 之后写死:
header("location: mains.php");
die();
尝试在代码顶部添加 ob_start();
,即在 error_reporting('E_ALL ^ E_NOTICE');
语句之前
首先回显$stmt->rowCount()
来检查它是否是returns一个值。如果 returns 值大于零,则在调用 header()
函数后添加 exit;
语句。
if ($stmt->rowCount() > 0) {
header("Location: mains.php");
exit;
}
else {
$er = "Some error occured please try again!";
}
// 编辑
我刚刚也注意到你 header 代码在 else 语句中,它可以 为真前的if语句引起的
您应该在 header 代码后加上 "die();" 所以 PHP 不会继续 运行 代码 并且可能会中断 header 的设置。 也请尝试使用绝对 URL 因为有时它会导致 PHP.
的问题代码:
header("Location: http://www.website.com/mains.php");
die();
尝试使用 javascript location.href
,如下所示:
echo '<script>location.href = "example.com/mains.php";</script>';
而不是
header("Location: mains.php");
关于您的评论:
thanks it worked but any php solution ? javascript may be disabled by user
echo '<script>';
echo 'window.location.href="mains.php"'; //Javascript Redirect
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url=mains.php" />'; //Incase of Javascript disabled
echo '</noscript>';