I am converting a Java Servlet web application to php.
How should I convert the following Java commands to php?
String temp = request.getParameter("q");
String temp2 = URLDecoder.decode(temp, "UTF-8");
Any help will greatly appreciated…
EDIT:
This is client code:
var myJSONText = playlist.serialize();
$.ajax({
type : 'POST',
url : "playlisthandler.php",
data : {
"q" : encodeURIComponent(myJSONText)
},
success : function(response) { ... },
error : function(response) { ... },
dataType : "json"
});
Are you saying that the encodeURIComponent is redundant?
You shouldn’t have the need to do so. The
request.getParameter()in servlet API and the$_REQUEST(and inherently also$_GETand$_POST) in PHP already do that based on the request character encoding. The need to do so indicates that the client side is doing it wrong by double-encoding the query string components.As per your code you’re indeed explicitly encoding the query string while jQuery already does that under the covers:
Remove the
encodeURIComponent()call so that thedatabecomes{ "q" : myJSONText }. jQuery will already take care that it will be URL-encoded. TheencodeURIComponent()is only necessary when you’re using plain vanillaXMLHttpRequest.