Jump to content

$_GET and spaces in the URL - Encoding Issue?


GuyFromTheNorth

Recommended Posts

I'm dynamically generating a URL to retrieve data from my MySQL database via an Ajax request with the GET method.  The variables/keys are countries that are taken from the database, and some have spaces in them (e.g. United States). 

 

When I pass these variable names to my sql query, those with spaces aren't matching because somewhere along the line underscores have replaced the spaces (those w/o spaces are matching just fine).  How do I ensure the spaces are preserved for when I make my MySQL query and return the results to the client?

 

I've tried using the escape function on the variable names when generating my URL in javascript, but that hasn't worked.  I've tried to find an answer w/ Google, but have found wildly contradictory guidance, none of which has worked yet for me.  I'm a relatively noob when it comes to scripting.  Thanks to anyone who can help me out.

You could use a urlencode() function that's 100% the same as PHP's.

 

For example, http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urlencode/.

 

Example usage:

 

function doSomething(country, state) {

    var url = 'something.php?county=' + urlencode(country) + '&state=' + urlencode(state);

}

 

 

Preserving the spaces is not a valid option since spaces are invalid characters in resource strings in the HTTP protocol.

I think I've found a simple solution.  Not particularly elegant, but it works for my immediate needs...

 

$regions = array_keys($_GET);

 

Then in my loop:

 

$temp = str_replace("_"," ",$regions[$i]);

 

Thanks for the help... I need to learn more about how to encode/decode strings from JS to PHP and vice-versa...

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.