0

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

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, the confusion was from me reading "Plus symbols ('+') are decoded to a space character." and not reading that "Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"]." Which is a bit annoying.
exactly, its a bit confusing on a first parse, but makes sense once you digest it.
0

Note:

Dots and spaces in variable names are converted to underscores.

(php.net - external variables)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.