GuyFromTheNorth Posted June 16, 2009 Share Posted June 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 16, 2009 Share Posted June 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
GuyFromTheNorth Posted June 16, 2009 Author Share Posted June 16, 2009 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.