dongyuyi5680 2015-12-30 04:32
浏览 34
已采纳

为什么我的代码没有将用户重定向到所需的页面? [重复]

This question already has an answer here:

The code below submits a post and redirects to another page. Post is submitted, rows are effected in database but I am not able to get redirected after submiiting 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!';
  }
} 
} 

If instead ofheader("Location: mains.php"); I use $er = Post success I see message after submitting post. So what is wrong with header here. Why code isn't redirecting to mains.php

</div>
  • 写回答

6条回答 默认 最新

  • dongzexi5125 2015-12-30 05:01
    关注

    With regard to your comment:

    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>'; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改