View Full Version : passing variable between PHP and javascript

02-28-2007, 04:43 PM
Dear all,

I face problem with passing value between PHP and Javacript.

for example:
in PHP, i encode the value with "urlencode" function and pass to javascript.

but when i decode with javascript "decodeURIComponent" or decodeURI" function may not all the string will return to original.

*string = "A B C"; ->encode with php function
in javascript show A+B+C -> i assign to a text box/ alert out.

i found out that JSON class can be solve my problem but problem was the system is not developed by me since started. This was quite difficult for me to integrated the JSON with my system.

isn't have list that show out the string the encode by PHP function may not able to decode by javascript?

Pls help. Thanks a lot.

03-01-2007, 02:09 PM
To be honest, I have no idea what it is you are trying to do.

You mentioned JSON so I assume you are using some variation of the AJAX paradigm. Remember that output from PHP is converted to a format that can be transferred with HTTP. This is generally HTML or plain-text but it can be JavaScript, CSS, PDF, etc..

JavaScript can read anything you pass through its internal "HttpRequest" object. (if the browser has one). You don't have to encode or decode text unless the format requires it. You can simply send a text string or XML or JSON or HTML.

You can also write javascript with PHP and then have your page either statically or dynamically load that file as a script src tag. Just make the file x.php and change the Content-type to text/javascript.