-1

Please bear up with me, I'm new to PHP. I'm using a mixture of PHP and jQuery on the same page. I need to pass variables from jQuery to PHP. All examples I came across describe how to pass variables to a PHP file on the server which I don't want. So far I manged to convert object to JSON using jQuery $.toJSON(); My question is: is it possible to pass jQuery data to php code if both jQuery and PHP reside on the same page?

Here is my code so far:

var myObject = {name: 'Tomas', age: 38};
var encoded = $.toJSON( myObject);
var name = $.evalJSON( encoded ).name;
var version = $.evalJSON(encoded).age;
4

1 に答える 1

3

No it is not possible since PHP is not a client side script but rather a server side script. What that means is that by the time the data is given to the client the PHP script would have most likely already finished running. The following demonstrates the relationship between PHP and JavaScript:

Server -> PHP -> Client (Browser) -> Javascript

Therefore, it is impossible to have javascript communicate to PHP on the same page. What you can do is use Ajax to call a server side file like you said - but I'm afraid that is as much as you can do in terms of PHP and JavaScript Communication

于 2013-03-24T23:09:41.980 に答える