Is there any way to do it without doing this: send javaScript variable to php variable
OR can I do that, and "cover up" my url to the original one, without refreshing the page(still keep the php variables)?
Is there any way to do it without doing this: send javaScript variable to php variable OR can I do that, and "cover up" my url to the original one, without refreshing the page(still keep the php variables)? |
|||||||||||||||||
|
You would want to wrap a lot of security around this code before putting it anywhere you care about, but it illustrates the principles without putting too much mud in the water:
|
||||
|
There's two points that you can use PHP to create javascript vars, the first being when the "page" is created on the server, the second point is during the operation of the javascript application (once the page is loaded). The second point will require some sort of client side request (ajax, websocket, etc). The best way to do it (in my experience) is using PHP's json extension which allows you to encode a PHP object/array into a json serialized string that can be unserialized/decoded within the browser into equivalent javascript types. To do this during page generation can be done similarly as follows:
Note that the eval occurs on client side within browser and is common in every major js library. Requesting an object during the normal operation of your javascript app will vary depending on how the data is requested, but each way will involve an asynchronous javascript request, a PHP script to handle the request (on the server side), and then a javascript side handler/callback that is called when data is received within javascript as a response to the request. I typically use PHP to echo a json_encode()'ed string as plain text, then code the javascript side response callback to decode the response and fire an event. For a basic example: PHP side:
javascript side:
The example above is very simple and generic to show how it works, but not much more is required for a fully functional solution. The real magic for a specific solution will happen within the "fire_event()" method - this is where the object can be handled via jquery or whatever. |
||||
|
A nice and easy way to do this is like this: PHP
Jquery
EXPLANATION Put the variable as a data attribute in some div using PHP, and then grab the data attribute from the DOM using JQuery. |
|||
|
I believe you are incorrect - you actually DO get the 'javascript' variable to PHP - using the jQuery code snippet below by @MikeD (jQuery is a javascript library containing many and many functions that you can then use in your code - making things little easier to do) above you can pass the javascript variable to PHP page. On the php page you can assign this variable (originating on client side - browser) to PHP variable using something as simple as this:
|
|||
|
If you use $.ajax to make the submission to php you won't need to refresh the page. The code for the example on that page would look like this
|
|||