0

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.

1
  • header always returns void. It makes no sense to put it in an if conditional. Commented Apr 14, 2020 at 21:33

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

-1

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.

3 Comments

Whatever for? I can't see how that would help.
You are right! Red it a bit fast based on the title and did not pay attention.
"when you check the URL" – They aren't checking the URL though, and its an HTTP redirect, they can't run JS inside it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.