Reputation: 15089
Consider the following PHP code:
//JS
// var a = { category: [9] };
// var x = $.param(a);
// window.location.href = "test.com/?foo=" + x;
$x = $_GET["foo"];
$x = "category%5B%5D=9"; // this is what $_GET["foo"] looks like
$result = array();
parse_str($x, $result);
var_dump($result);
Now, that will output:
array(1) {
["category"]=>
array(1) {
[0]=>
string(1) "9"
}
}
Which is wrong. The element inside the category
array should be an integer and not a string. Why is parse_str
handling that input so badly? And how can I make it convert to string only the data that is between "
or '
? (pretty much how json_decode
works)
Upvotes: 1
Views: 395
Reputation: 158090
If you are using plain GET, how should parse_str()
know about the desired data type? You, the programmer, need to:
parse_str()
will always deliver strings.If you want to transfer strictly typed values between the client and the server, you need to go a more sophisticated way . Starting from json, over XMLRPC to SOAP.
Using json in the request might be the simplest way to start, however it does not fit well for GET
requests since it is hard to type that in an url, sure you do that by code, however I still think json
doesn't suite for GET requests.
Upvotes: 2