Categories: javascript, php, html

PHP: How to use Javascript inside PHP code

3 answers

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.

All answers to this question, which has the identifier 61217544

The best answer:

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.

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.

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.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data