Harsh
Harsh

Reputation: 2096

AJAX POST and Plus ( + ) and (&) Sign How to Encode?

I'm POSTing the contents of a form field via AJAX to a PHP script and using this code

if(!http)
            http = CreateObject();  

        nocache = Math.random();

        http.open('post', 'addvm.php');
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");
        http.onreadystatechange = SaveReply;

        http.send(params);

The problem is that any + and & signs are being stripped out and replaced. How can I safely 'encode' the + and & signs and then appropriately 'decode' it on the PHP side?

Upvotes: 1

Views: 1886

Answers (2)

Harsh
Harsh

Reputation: 2096

need to encode your params encodeURIComponent Note:-When you access $_GET, $_POST or $_REQUEST in PHP, you are retrieving values that have already been decoded.

Upvotes: 2

metaforce
metaforce

Reputation: 1377

try:

js: encodeURIComponent (/* url */);

php: urldecode (/* url */);

Upvotes: 1

Related Questions