Webdevelopment

How to integrate javascript in your php code

If you do not wish to have the help of a php development company, here are some ideas to show you how to combine the two languages ​​to take full advantage of their complementarity.

Between visitor and server

For a classic HTML page, the exchange is very simple. Indeed, the visitor will send a request to the server based on the URL in the address bar. The latter will then read the index.html file that is already stored on its hard drive and then sends it directly as is the visitor. The browser will thus receive the same fichir interpreting the HTML and JavaScript code it contains and by displaying it on the screen of the visitor.

For a PHP page, the server will not just play the role of the factor. The visitor sends the same request in the same way. The server will open the index.php3 fichir his repertoire. The extension is quite different for the server to know that there is an additional action to perform. Thus, there is an instruction successsion that the server will need to run. The result of this script will then be a file that contains an HTML and JavaScript code. The latter will be returned to the browser of the visitor. An additional step is then performed between the request and sending the page. These treatments may be of any type. The server has the ability to extract the latest news on the site, count the number of visitors connected, the display on the page and others. Not only do these treatments will work even when the server but they will consume memory and processor time. That is why the free hosts do not offer in any case as servers languages ​​and often the php.

The order of execution

PHP page contains a PHP script that will first be read and executed by the server. The result will then be sent to the browser. JavaScript present in the result will be interpreted after the PHP code. Here the aim is to generate PHP HTML browser that the visitor can interpret and display. It is alor simpossible Javascript to call functions or PHP variables. In php you can not use Javascript because php code is no longer visible on the generated page.

React !