By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,550 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,550 IT Pros & Developers. It's quick & easy.

problem with sending json encoded data via ajax to a php script(internationalization)

P: n/a
Hi,

I am using javascript to collect the user input from a form, put it in
an object, json encode it (using JSON or YUI JSON), use the YUI
connection manager (XMLHTTPRequest) to send it to a PHP script for
validation after a json_decode; result an associative array. The
webpage (valid XHTML), the PHP script, the Apache webserver, the mysql
db server all use charset utf-8.

It works ok for normal text, problems began when one of the input
fields contained coordinates (5617'16'', it is supposed to contain
coordinates), the PHP script could not json_decode the data send by
IE7 (no problem with FF, Opera and Safari).
When the user input contains an &, then the PHP script cannot not
json_decode it, no error message, just an empty array.

I guess the solution might be in the use of escape (javascript) and
urldecode (PHP), but I have not succeeded in making it work yet. Do
you use those functions and the data you send, on the querystring or
on the complete url? Other problem is that escape and urldecode are
not an exact match.

Pugi!
Jan 9 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Pugi! said:
I guess the solution might be in the use of escape (javascript) and
urldecode (PHP), but I have not succeeded in making it work yet. Do
you use those functions and the data you send, on the querystring or
on the complete url? Other problem is that escape and urldecode are
not an exact match.
use encodeURIComponent in javascript before you assemble as JSON, and
then send it via post through the XHR.

use XMLHttpRequest.setRequestHeader(
"Content-Type", "application/x-www-form-urlencoded")

To set up your XHR for POST, then assemble the data you want to send in
this format:

"name=value&name2=value2&....nameN=valueN"

And where you would normally send null in your XHR, send the data instead.

The great thing about encodeURIComponent() is that all that translation
is done at the server level on most servers (all the ones I've ever
worked on), so once it gets to PHP, it should be okie doke.

If not, contact me privately (the email is in my sig), and we can talk
about the PHP side, this isn't the place for that.

All the best,
~A!

--
anthony at my pet programmer dot com
Jan 9 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.