is there any method by which i can convert javascript variables to php variables
Converting JavaScript variable to php
Collapse
X
-
Tags: None
-
Most of the begginers try to do this?
It doesn't make any sense assigning javascript variables to PHP variables. You can pass the values of javascript variables to a PHP script, where you can get those values into PHP variables. There are different ways of passing values from client (browser) to PHP script -
Javascript can not interact with PHP directly.Originally posted by Shalini Bhallais there any method by which i can convert javascript variables to php variables
This is due to the fact that Javascript is run on the client's browser (client-side) while PHP is executed on the server (server-side).
So, before your Javascript code is even read by the browser, all of the PHP code will already have been executed by the server.
There are two ways to have Javascript communicate with PHP.
Either you have to refresh/redirect the page, sending along the data via the POST or GET protocols.
Or, you can use AJAX.
I would still be very careful when using data sent by Javascript. As it is generated by the client's browser, it can easily be manipulated before it is sent.
Make sure you validate it before you use it.Comment
-
Not,
u can use php variable in javscript but cant use javascript variable in PHP because php can not parrsing the javascript value....
Regards,
Sandeep agarwalComment
-
I'm guessing hes talking about just echoing PHP values into the Javascript code?Originally posted by r035198xHow ?
For the record, using that method, the Javascript code is in fact not using a PHP variable. The PHP code is used to create a Javascript variable, which Javascript can then use.Comment
-
"Converting " a variable from JavaScript to PHP or vice versa is kind of like trying to "convert" a 20-Euro note into a butternut squash. You have to go through the grocer, or else you're bound to get some funny looks.
And for 20 Euros, it better be one heckuva butternut squash. I'd say this is about the point where the analogy breaks down.
Anyway, PHP is executed on the server side, whereas JavaScript works on the client side, and the two *never talk to each other*.
In fact, PHP technically doesn't even know that the User's computer even exists; the HTTP server handles all those details.
PHP can output JavaScript code that (once the User's browser executes that code) will create a JavaScript variable with a particular value.
You can also pass parameters in an HTTP request that PHP converts into elements in the $_GET superglobal.
But the "conversion " process has to be done in a roundabout way because, as noted before, PHP and JavaScript don't talk to each other, and in fact, they are not even aware of each other's existence.Comment
-
Brilliant analogy :)Originally posted by pbmods"Converting " a variable from JavaScript to PHP or vice versa is kind of like trying to "convert" a 20-Euro note into a butternut squash. You have to go through the grocer, or else you're bound to get some funny looks.
Although I am not really sure what a butternut squash is.Comment

Comment