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

Discussion in 'Javascript' started by Pugi!, Jan 9, 2008.

  1. Pugi!

    Pugi! Guest

    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!
    Pugi!, Jan 9, 2008
    #1
    1. Advertising

  2. 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
    Anthony Levensalor, Jan 9, 2008
    #2
    1. Advertising

  3. Pugi!

    Pugi! Guest

    On 9 jan, 16:29, Anthony Levensalor <>
    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);
    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!
    Pugi!, Jan 9, 2008
    #3
  4. *** Pugi! *** wrote a whole bunch of nifty stuff On 1/9/2008 1:35 PM:
    > On 9 jan, 16:29, Anthony Levensalor <>
    > 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
    Anthony Levensalor, Jan 9, 2008
    #4
  5. Pugi!

    Pugi! Guest

    On 9 jan, 19:49, Anthony Levensalor <>
    wrote:
    > *** Pugi! *** wrote a whole bunch of nifty stuff On 1/9/2008 1:35 PM:
    >
    >
    >
    > > On 9 jan, 16:29, Anthony Levensalor <>
    > > 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!
    Pugi!, Jan 10, 2008
    #5
  6. On 1/10/2008 3:44 AM, Pugi! wrote:
    > On 9 jan, 19:49, Anthony Levensalor <>
    > wrote:
    >> *** Pugi! *** wrote a whole bunch of nifty stuff On 1/9/2008 1:35 PM:
    >>
    >>
    >>
    >>> On 9 jan, 16:29, Anthony Levensalor <>
    >>> 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
    Anthony Levensalor, Jan 10, 2008
    #6
  7. Pugi!

    David Mark Guest

    On Jan 9, 1:35 pm, "Pugi!" <> wrote:
    > On 9 jan, 16:29, Anthony Levensalor <>
    > 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);


    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.)
    David Mark, Jan 12, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajive Narain
    Replies:
    0
    Views:
    1,580
    Rajive Narain
    Sep 18, 2009
  2. Florian Frank
    Replies:
    0
    Views:
    231
    Florian Frank
    Jun 30, 2009
  3. Otto Wyss

    Ajax sample code with Json/PHP

    Otto Wyss, Apr 9, 2007, in forum: Javascript
    Replies:
    5
    Views:
    330
    Otto Wyss
    Apr 10, 2007
  4. Replies:
    1
    Views:
    128
    Thomas 'PointedEars' Lahn
    Feb 23, 2008
  5. sajuptpm
    Replies:
    2
    Views:
    317
    sajuptpm
    Dec 28, 2012
Loading...

Share This Page