Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How do I encode string for request?

    I'm using AJAX but I've got that fairly down, I'm a newbie to Javascript and just learning PHP and fail to see the problem in how to send a string using an AJAX request properly..

    My conflicting code is this:
    var url="processor.php?data=" + document.getElementById('inputbox').value;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

    It'd send inputbox's contents as a string to processor.php to process a change to it.. I tried something like this:

    var url="processor.php?data=" + escape(document.getElementById('inputbox').value);

    And in the php: urldecode($_GET['data']); but spaces come up as ascii character 01
    Thisisastringwithspaces

    How can I encode the string in javascript, in a compatable method that PHP would receive?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...you do not NEED to decode! $_GET does that *for* you!

    Try using just $_GET["data"] and see if it isn't already right.

    See, when you decode the already-decoded spaces, who knows what's going to happen??

    Not really a PHP person, but that's how it works in JSP and ASP and ASP.NET. The system has already done the decoding for you when you use the equivalent of $_GET in all those systems.

  • #3
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...you do not NEED to decode! $_GET does that *for* you!

    Try using just $_GET["data"] and see if it isn't already right.

    See, when you decode the already-decoded spaces, who knows what's going to happen??

    Not really a PHP person, but that's how it works in JSP and ASP and ASP.NET. The system has already done the decoding for you when you use the equivalent of $_GET in all those systems.
    Nope I still get the weird characters, any and no encoding.. Very strange..

    I'll assume maybe my text parsing function doesn't like the text that's sent, and parses in those weird characters with it.. I'll use a preg_replace or str_ireplace function to remove the weird ascii non-printable characters and replace with spaces, Very interesting error I may never know how it originated..

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    DOH! Right you are.

    Wow. Wonder how I never noticed that before!!!

    ASP handles %20 and + the same. Which is fine, until you have a + in the escape() !!

    This works, but is pretty hacky:
    Code:
    function urlencode(str)
    {
        str = str.replace(/\+/g,String.fromCharCode(8));
        str = escape(str);
        str = str.replace(/\%20/g,"+");
        str = str.replace(/\%08/g,"%2B");
        return str;
    }
    Seems to me I saw a better way floating around here on the forums recently.

  • Users who have thanked Old Pedant for this post:

    oni-kun (06-09-2009)

  • #5
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    DOH! Right you are.

    Wow. Wonder how I never noticed that before!!!

    ASP handles %20 and + the same. Which is fine, until you have a + in the escape() !!

    This works, but is pretty hacky:
    Code:
    function urlencode(str)
    {
        str = str.replace(/\+/g,String.fromCharCode(8));
        str = escape(str);
        str = str.replace(/\%20/g,"+");
        str = str.replace(/\%08/g,"%2B");
        return str;
    }
    Seems to me I saw a better way floating around here on the forums recently.
    Perfect, I managed to get rid of all the difficulties with that, and fixed my PHP function to double check for HTMLspecialchars and remove them so it parses 100% clean.

    It all works now.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •