I would like to run one of JS scripts while redirecting to another webpage .. Script to change a variable in Index.php like below code:
if ($flag == 1) {
// echo "<h1> Welcome to Website Home Page </h1>";
if(header("Location: ../../index2.php")) {
echo "<script type='text/javascript'> document.getElementById(body).style.color = 'red'; </script>";
};
} else {
// echo "<h1>Try Again!!!</h1>";
echo "<div class='alert alert-danger' role='alert'>
Your are not a Registered User <i class='fas fa-exclamation-triangle'></i> <br> Please Use below registrarion form.</div>";
}
N.B: Still Junior learning PHP.
3 Answers 3
An HTTP redirect tells the browser that it should get the data it asked for from a different URL.
The body of a redirect response should be a message telling the user that they need to request a different URL. It is used in clients which either don't have HTTP redirect support or which have it disabled. Today such clients are practically non-existent.
If you want to run JS on the page being redirected to then you need to embed that JS in that page.
Comments
The header function not is for check URL, this function for send HTTP header.
For check url path using REQUEST_URI in superglobal variable $_SERVER.
REQUEST_URI - the URI which was given in order to access this page; for instance, '/index.html'.
More: https://www.php.net/manual/en/reserved.variables.php
Run the following code for research:
echo '<pre>';
var_dump($_SERVER)
About your JS code: You forgot to enclose the body in quotation marks
I think it will help you.
Comments
Add a slash before all single quote ' --> \'
But above code dose not look ok...
You should add an event Listener, onclick, on something(when you check the URL) and then include your js in a function.
headeralways returnsvoid. It makes no sense to put it in anifconditional.