Bluehost.com Web Hosting $6.95

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

This is a discussion on Re: problem with sending json encoded data via ajax to a php script(internationalization) within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Pugi! said: > I guess the solution might be in the use of escape (javascript) and > urldecode (PHP), but ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-09-2008
Anthony Levensalor
 
Posts: n/a
Default Re: problem with sending json encoded data via ajax to a php script(internationalization)

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
Reply With Quote
  #2 (permalink)  
Old 01-09-2008
Anthony Levensalor
 
Posts: n/a
Default Re: problem with sending json encoded data via ajax to a php script(internationalization)

*** Pugi! *** wrote a whole bunch of nifty stuff On 1/9/2008 1:35 PM:
> On 9 jan, 16:29, Anthony Levensalor <killf...@mypetprogrammer.com>
> 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!





--
anthony at my pet programmer dot com
Reply With Quote
  #3 (permalink)  
Old 01-10-2008
Anthony Levensalor
 
Posts: n/a
Default Re: problem with sending json encoded data via ajax to a php script(internationalization)

On 1/10/2008 3:44 AM, Pugi! wrote:
> On 9 jan, 19:49, Anthony Levensalor <killf...@mypetprogrammer.com>
> wrote:
>> *** Pugi! *** wrote a whole bunch of nifty stuff On 1/9/2008 1:35 PM:
>>
>>
>>
>>> On 9 jan, 16:29, Anthony Levensalor <killf...@mypetprogrammer.com>
>>> 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!
>>
>> --
>> anthony at my pet programmer dot com

>
> 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!
--
anthony at my pet programmer dot com
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:36 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0