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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度