I'm going to try a simpler answer: Show Explanation of the problemFirst, let's understand the flow of events when a page is served from our server:
So really, the core thing to remember here is that HTTP is stateless. Once a request left the server, the server can not touch it. So, that leaves our options to:
SolutionsThat's the core question you should be asking yourself is: Am I writing a website or an application?Websites are mainly page based, and the page load times needs to be as fast as possible (for example - Wikipedia). Web applications are more AJAX heavy and perform a lot of round trips to get the client fast information (for example - a stock dashboard). WebsiteSending more requests from the client after the initial request is done is slow as it requires more HTTP requests which have significant overhead. Moreover, it requires asynchronousity as making an AJAX request requires a handler for when it's complete. I would not recommend making another request unless your site is an application for getting that information from the server. You want fast response times which have a huge impact on conversion and load times. Making Ajax requests is slow for the initial uptime in this case and unneeded. You have two ways to tackle the issue
Setting a cookie is really not very difficult, you just assign it a value:
Then, you can
read it with JavaScript using Here is a short hand rolled parser, but the answer I linked to right above this has better tested ones:
Cookies are good for a little data. This is what tracking services often do. Once we have more data, we can encode it with JSON inside a JavaScript variable instead:
Assuming ApplicationIf you're writing an application - suddenly the initial load time isn't always as important as the ongoing performance of the application, and it starts to pay off to load data and code separately. My answer here explains how to load data using AJAX in JavaScript:
Or with jQuery:
Now, the server just needs to contain a
This way, our JavaScript file asks for the data and shows it rather than asking for code or for layout. This is cleaner and starts to pay off as the application gets higher. It's also better separation of concerns and it allows testing the client side code without any server side technology involved which is another plus. Postscript: You have to be very aware of XSS attack vectors when you inject anything from PHP to JavaScript. It's very hard to escape values properly and it's context sensitive. If you're unsure how to deal with XSS, or unaware of it - please read this OWASP article, this one and this question. How can I pass PHP variable to JavaScript?We can pass data from PHP to JavaScript in two ways depending on the situation. First, we can pass the data using the simple assignment operator if we want to perform the operation on the same page. Else we can pass data from PHP to JavaScript using Cookies. Cookie work in client-side.
Can I access PHP variable in JavaScript?We can use the short PHP echo tag inside the JavaScript to pass the PHP variable to JavaScript.
How store JavaScript value in PHP?After the execution of the javascript code and after assigning the relevant value to the relevant javascript variable, you can use form submission or ajax to send that javascript variable value to use by another php page (or a request to process and get the same php page).
How pass data from PHP to JavaScript using AJAX?ajax({ type: 'POST', url: 'process. php', data: { text1: val1, text2: val2 }, success: function(response) { $('#result'). html(response); } }); });
|