Solution 1 :

You simply can’t do location and refresh at the same time, so you have to separate both headers, i.e. the refresh should be part of the page where you are displaying the message. Also, there is no reason (it’s a bad practice actually) to pass entire HTML into the URL, better use some ID, like:

db.php

<?php
    if(mysqli_query($link, $sql)){
        header("Location: addbusiness.php?message=1");
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
?>

addbusiness.php

<?php
    if(!empty($_GET['message'])){
        if($_GET['message'] == 1){
            header( "refresh:5;url=index.php" );
            echo "<div class='alert alert-success' role='alert' style='text-align: center; margin-bottom: 50px;'>Success.</div>";
        }else{
            //do something else
        }
    }
?>

Based on such a condition, you also will avoid the refresh every time when visits addbusiness.php

Of course, in any case you have to ensure that there is no any HTML output to the browser before calling header(). In both pages.

Problem :

I’m using a header to show succes message after submit. I also want to redirect the user to the index.php page after 5 seconds, but somehow it doesn’t work.

if(mysqli_query($link, $sql)){
    header("Location: addbusiness.php?message=<div class='alert alert-success' role='alert' style='text-align: center; margin-bottom: 50px;'>Succes.</div>");
    header( "refresh:5;url=index.php" );
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

Comments

Comment posted by mitkosoft

second header with

Comment posted by Mower

If i place it into adbusiness.php it will refresh every time I’m using that page. I don’t want that.

Comment posted by mitkosoft

you could use the same condition when to display a message, to do

Comment posted by How to get the error message in MySQLi?

Please read

Comment posted by Mower

Thank you for your clearly understandable answer. I’ll use this id stuff, but there isn’t any way to display a message and after that refresh the page? Because it’s not an option for me to refresh it every time.

Comment posted by mitkosoft

the script will do exactly that – will display the message and after that will do a rafresh. And will do it ONLY if you call

Comment posted by Mower

I get this error message:

Comment posted by mitkosoft

As I mentioned in my answer, this code must be placed before any HTML output, best on the top of the page.

Comment posted by Mower

Sorry, I missed that. Now it’s working fine. Thanks for your help. Accepted your answer.

By