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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.