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

P

Pugi!

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 (56°17'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!
 
A

Anthony Levensalor

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!
 
P

Pugi!

Pugi! said:


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!

This really was very helpful.
This is how I use it:
- clientside (javascript):
var data = new Object();
data.field1 =
encodeURIComponent(document.formname.field1.value.trim());
...
qs = YAHOO.lang.JSON.stringify(data);
...
YAHOO.util.Connect.asyncRequest('GET', 'mywebpage.php?data='+qs,
callback);

- serverside (PHP)
$data = json_decode(stripslashes(sanitize($_GET['data'])), true);
and I get an associative array, just like I wanted.
(sanitize is a custom function that removes trailing whitespace and
html tags among other things)

The YUI Connection manager uses setRequestHeader = ("Content-Type",
"application/x-www-form-urlencoded") and utf-8 by default, you cannot
change it. I've tested it with FF2, IE7, Safari and Opera using ° & @
' ë ô $ £ EURO + µ ... and it works. The only thing I found that prevents
the JSON decode is a double quote.

Thankx,

Pugi!
 
A

Anthony Levensalor

*** Pugi! *** wrote a whole bunch of nifty stuff On 1/9/2008 1:35 PM:
Pugi! said:

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!

This really was very helpful.
This is how I use it:
- clientside (javascript):
var data = new Object();
data.field1 =
encodeURIComponent(document.formname.field1.value.trim());
...
qs = YAHOO.lang.JSON.stringify(data);
...
YAHOO.util.Connect.asyncRequest('GET', 'mywebpage.php?data='+qs,
callback);

- serverside (PHP)
$data = json_decode(stripslashes(sanitize($_GET['data'])), true);

Just do the json_decode call first, and then do the sanitizing and
stripslashing and the like. That should solve your quotes problem.

Glad I could help!

~A!
 
P

Pugi!

*** Pugi! *** wrote a whole bunch of nifty stuff On 1/9/2008 1:35 PM:


This really was very helpful.
This is how I use it:
- clientside (javascript):
var data = new Object();
data.field1 =
encodeURIComponent(document.formname.field1.value.trim());
...
qs = YAHOO.lang.JSON.stringify(data);
...
YAHOO.util.Connect.asyncRequest('GET', 'mywebpage.php?data='+qs,
callback);
- serverside (PHP)
$data = json_decode(stripslashes(sanitize($_GET['data'])), true);

Just do the json_decode call first, and then do the sanitizing and
stripslashing and the like. That should solve your quotes problem.

Glad I could help!

~A!

Without stripslashes no json_decode.
Input like c:\my documents\test\test.pdf doesn't look to good either.

Pugi!
 
A

Anthony Levensalor

*** Pugi! *** wrote a whole bunch of nifty stuff On 1/9/2008 1:35 PM:


On 9 jan, 16:29, Anthony Levensalor <[email protected]>
wrote:
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
This really was very helpful.
This is how I use it:
- clientside (javascript):
var data = new Object();
data.field1 =
encodeURIComponent(document.formname.field1.value.trim());
...
qs = YAHOO.lang.JSON.stringify(data);
...
YAHOO.util.Connect.asyncRequest('GET', 'mywebpage.php?data='+qs,
callback);
- serverside (PHP)
$data = json_decode(stripslashes(sanitize($_GET['data'])), true);
Just do the json_decode call first, and then do the sanitizing and
stripslashing and the like. That should solve your quotes problem.

Glad I could help!

~A!

Without stripslashes no json_decode.
Input like c:\my documents\test\test.pdf doesn't look to good either.

Pugi!

Ok, can you send me a php snippet? Run the sig all together and
translate the at and dot, shoot me an email, I'll take a closer look.

~A!
 
D

David Mark

This really was very helpful.
This is how I use it:
- clientside (javascript):
var data = new Object();
data.field1 =
encodeURIComponent(document.formname.field1.value.trim());
...
qs = YAHOO.lang.JSON.stringify(data);
...
YAHOO.util.Connect.asyncRequest('GET', 'mywebpage.php?data='+qs,
callback);

This makes no sense to me. Why are you using the JSON stringify
function to build a querystring? Just encode the name/value pairs and
join with ampersands.
- serverside (PHP)
$data = json_decode(stripslashes(sanitize($_GET['data'])), true);

The use of the stripslashes function clearly indicates a problem
(which isn't surprising.)
and I get an associative array, just like I wanted.

Just like you could get from a normal query consisting of name/value
pairs.
(sanitize is a custom function that removes trailing whitespace and
html tags among other things)

The YUI Connection manager uses setRequestHeader =  ("Content-Type",
"application/x-www-form-urlencoded") and utf-8 by default, you cannot
change it. I've tested it with FF2, IE7, Safari and Opera using ° & @
' ë ô $ £ EURO + µ ... and it works. The only thing I found that prevents
the JSON decode is a double quote.

Which indicates that there is a problem with your form serialization
method (or the server script that attempts to read the values.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top