0

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

bɪˈɡɪnə
  • 1,087
  • 2
  • 23
  • 46

6 Answers6

1

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>'; 
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

You need to write die soon after header like this:

header("location: mains.php");
die();
Maha Dev
  • 3,915
  • 2
  • 31
  • 50
0

Try adding ob_start(); at the top of the code i.e. before the error_reporting('E_ALL ^ E_NOTICE'); statement

0

First echo the $stmt->rowCount() to check whether it returns a value. If it returns value larger then zero then add exit; statement after calling header() function.

if ($stmt->rowCount() > 0) {
   header("Location: mains.php");
   exit;
} 
else {
   $er = "Some error occured please try again!";
}
Sazzadur Rahman
  • 2,650
  • 1
  • 18
  • 34
0

// Edit

I have also just noticed you header code is in a else statement, it could be caused by the if statement before being true.

You should put "die();" after your header code so PHP doesn't continue running the code and possibly interrupting setting the header. and also please try using an absolute URL as sometimes it can cause problems with PHP.

Code:

header("Location: http://www.website.com/mains.php");
die();
K. Bishop
  • 72
  • 6
0

Try using javascript location.href like below:

echo '<script>location.href = "example.com/mains.php";</script>';

Instead of

header("Location: mains.php");
Amit Rajput
  • 2,061
  • 1
  • 9
  • 27