TigerBear
TigerBear

Reputation: 2834

Encoded JQuery Url not decoding correctly in PHP

I am using .ajax() to send a request to the server. The server is using PHP to process the request.

According to php urldecode, $_REQUEST is already decoded and Plus symbols ('+') are decoded to a space character.

What I have found is that Plus symbols are being decoded to a underscore ('_'). This is true for both + and %20. Is there any way around this? This seems like unexpected behavior.

Code sample for what its worth:

ajax:

$.ajax({
       url: 'mySite.php',
       method: 'POST',
       data: $(this).serialize()
    });

php:

$myVar = "Veh #";
if (isset($_REQUEST["$myVar"])){
//do stuff
}
//to see request
var_dump($_REQUEST);

The var_dump gives

array(1) {["Veh_#"]=> string(1) "6"}

I would expect is to be

array(1) {["Veh #"]=> string(1) "6"}

fiddler data posted:

Veh+%23=6

Upvotes: 0

Views: 173

Answers (2)

TheWolf
TheWolf

Reputation: 1385

Note:

Dots and spaces in variable names are converted to underscores.

(php.net - external variables)

Upvotes: 0

bigmadwolf
bigmadwolf

Reputation: 3519

I may be incorrect as I'm still learning PHP, but I think this is standard behaviour when using GET and POST in PHP.

see here in the documentation

http://www.php.net/manual/en/language.variables.external.php

I not aware of anyway around this.

also see this stack overflow question

Get PHP to stop replacing '.' characters in $_GET or $_POST arrays?

Upvotes: 1

Related Questions