Click to See Complete Forum and Search --> : [RESOLVED] AJAX: Data after ampersand (&) lost


rapmonkey
01-01-2007, 10:54 PM
Hello,

I have a script that takes data from a web form, and submits the data to a PHP script using POST through AJAX. The script works successfully in all cases but one. If there is an ampersand (&) in the data being POSTed, the ampersand and all data that comes after it does not make it to the PHP script. An example of this is submitting a hyperlink with an & character in it. I haven't the slightest clue why this is happening. It's your standard AJAX script. And yes, the ampersand IS escaped, being submitted as "&". Any ideas why this may be happening?

Thanks for any help!!

bradgrafelman
01-01-2007, 11:24 PM
the ampersand IS escaped, being submitted as "&"But that's not the proper way to encode an ampersand in POST'ed data! An ampersand separates POST'ed data in the string, so by trying to avoid the '&' breaking your data, you're trying to rewrite it as '&' ... but notice, you still have an ampersand in there?

You were right in thinking it had to be encoded... you just weren't encoding it in the correct way. To encode data properly for POST strings, you need to encode them as '%xx' where xx are hex digits. For example, an ampersand is properly encoded as '%26'. In PHP, this is done with the urlencode() function. I can't remember the JS function off the top of my head... is it possibly encode() ?

EDIT: Drats, I was close... it's the escape() function in Javascript.

EDIT2: I saw you were searching the board for applicable keywords relating to your problem... just thought it was nice to see posters doing research on their problems instead of just pleading for help :)

rapmonkey
01-01-2007, 11:33 PM
This worked! I knew it had to be something simple. Thanks so much for the quick and detailed reply!!

bradgrafelman
01-01-2007, 11:34 PM
Glad I could help! Don't forget to mark this thread resolved.

EDIT: And you didn't. Wow. Searched the board *and* marked the thread resolved. It was nice helping you, rapmonkey :)