Codificación de URL para PHP

I am trying to send a simple URL request to a server with XML with something along the lines of"GET","serverFile.php?q="+str,true);

I have three variables contained in the javascript file, i.e.:

var userName = Steve;
var findtype = Green Holder;
var findcategory = B Provincial;

I was of trying to "package" the strings together into somewhat

var combinedString = "find&userName=" + userName + "&type=" + findType + "&category=" + findCategory;

and then calling the function above"GET","serverFile.php?q="+combinedString,true);

but I'm confused how to decode this on the server side or whether this would work at all. I was originally thinking of having an if statement:

if ($q == "find")
    $category=$_GET["category"]; other coding here to get data

But I'm very new to PHP so I have no idea if this is the right approach. I was trying to figure out appropriate URL encoding online but is the combined string in the appropriate form? Would the "&" appropriately divide the contents or would I need & and would I need to place a plus sign between spaces or would it do that by itself? Any information would be helpful!

preguntado el 09 de marzo de 12 a las 17:03

1 Respuestas

You need to encode your URL components in JavaScript before you use string concatenation.

combinedString = ... + encodeURIComponent(userName) + ... + encodeURIComponent(findType) + ...

PHP will automatically fill $_GET with the url decoded values, so if you were to pass:


PHP would automatically decode $_GET['foo'] A igual "/@#$"

En cuanto a tu & comment, you only ever need to escape XML when you're using the value in XML (or HTML in this case, which is close but not quite identical).

If you're simply typing in the browser's location bar, you'd use:


But if you were marking this up in an anchor element (<a>) you'd use:

<a href="somepage.php?foo=bar&amp;fizz=buzz">lorem ipsum</a>

respondido 09 mar '12, 17:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.