Saoud ElTelawy
Saoud ElTelawy

Reputation: 294

PHP: How to use Javascript inside PHP code

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.

Upvotes: 0

Views: 73

Answers (3)

Ararat
Ararat

Reputation: 143

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.

Upvotes: 1

Thomas_krk
Thomas_krk

Reputation: 224

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.

Upvotes: -1

Quentin
Quentin

Reputation: 944425

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.

Upvotes: 4

Related Questions