0

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);
}
Mower
  • 177
  • 2
  • 10
  • 2
    second header with `refresh` has to be placed into `addbusiness.php` page actually. also there is no reason to send ***entire*** HTML message, just send an ID or some other param and display corresponding message, based on that into your next page. – mitkosoft Feb 18 '20 at 10:21
  • If i place it into adbusiness.php it will refresh every time I'm using that page. I don't want that. – Mower Feb 18 '20 at 10:26
  • you could use the same condition when to display a message, to do `refresh`, see below.. – mitkosoft Feb 18 '20 at 10:32
  • Please read [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Feb 22 '20 at 21:59

1 Answers1

0

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.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31
  • 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. – Mower Feb 18 '20 at 10:37
  • 1
    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 `addbusiness.php` with `GET` parameter `message=1` otherwhise, the page will be loaded normally. – mitkosoft Feb 18 '20 at 10:39
  • I get this error message: ```Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\vallalkozok-v2\addbusiness.php:1) in C:\xampp\htdocs\vallalkozok-v2\addbusiness.php on line 112``` but there isn't any php code in the first line. – Mower Feb 18 '20 at 10:42
  • 1
    As I mentioned in my answer, this code must be placed before any HTML output, best on the top of the page. – mitkosoft Feb 18 '20 at 10:44
  • 1
    Sorry, I missed that. Now it's working fine. Thanks for your help. Accepted your answer. – Mower Feb 18 '20 at 10:46