PHP code is executed on the server side and output data are transmitted to Web browser.
2) By adding in the JS script within the HTML document only the necessary data processed by PHP.
1. Alert window with the user nameIn a web site with authentication system to be displayed an alert window with the user name, after he logged in.
In this case we assume that the username is stored in a session variable ($_SESSION['name']).
Using the first method, would be the following PHP code:
Or the second way:
To understand better, try the following example:
a) Create a php file on the server (named "phpjs_test.php") and add the following code:
b) In the same folder on the server create a html file (ex. "test_jsphp.html") where you add the fallowing code:
- Call this html file on the server. The result will be a link (written by "document.write()") defined in php script, based on the 'id' added in the URL in "src" attribute.