1

After a form submit, I am redirecting to the mentioned header location using below codes:

PHP Part:

$TestMessage = "This is a sample message. This is not a constant value. Original data will be collecting from DB";

if(mysqli_affected_rows($conn) > 0) {   

header('Location:mydaomain/expenses?success=1');    

}else{

header('Location:mydomain/expenses?failed=1');

} 

HTML Part:

<div class="SubResult" id="SubResult" >

    <?php 

        if ( isset($_GET['success']) && $_GET['success'] == 1 )

            {
                echo $TestMessage;
                echo "<h1 style='color:green;padding:3%;'>Success !!</h1>";
            }

        if ( isset($_GET['failed']) && $_GET['failed'] == 1 )

            {
                echo $TestMessage;
                echo "<h1 style='color:red;padding:3%;'>SQL Error !!</h1>";
            } 
    ?>  

</div> 

Issue with the above code is,HTML wont display $TestMessage PHP variable content.

Why I am using this header option in PHP:

Earlier, form was getting submitted whenever I refresh the page after an original submit. I found this solution to avoid that issue. When I remove this header part from php file, I am able to echo variable to PHP (But at that I am facing issue with the form submission on page refresh issue)

Is there any way I can echo this variable to HTML ?

acr
  • 1,674
  • 11
  • 45
  • 77

1 Answers1

1

When you use header, you are doing a redirect, so your variable $TestMessage is not available after that, if you want to use it, make sure you pass using query string, for example:

header('Location:mydaomain/expenses?success=1&testMessage='.$TestMessage);   

and in the next page use $_GET['testMessage'] to get his value.

Sergio Susa
  • 249
  • 3
  • 7