PHP: Talk to me Javascript!

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

So I have a textarea on a page. When a user clicks the submit button, Javascript grabs the content in the textarea and sends it off to a PHP file using an XMLHttpRequest object. How can I, using Javascript, encode the content in the textbox, and then decode it on the PHP page?

Is there a decode/encode function that works in JS and PHP? I've tried quite a few functions, and I'm not having much luck!

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

According to some commenter on the PHP doc pages, JS's encodeURIComponent and PHP's rawurlencode perform the same type of encoding. So I can use encodeURIComponent in Javascript and rawurldecode in PHP.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

What are you needing it encoded for?

-Greg

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Because if someone enters "this is some text and this could through off the program &some_param=hahahah4" and it's not encoded, it could throw off the PHP script. It's not necessarily a security risk for my program, but it could produce some unexpected output for the user, which I want to avoid.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.