gevans Posted September 24, 2009 Share Posted September 24, 2009 Is the resulting text from escape() in JavaScript identical to the textual result of urlencode() in php? I'm sending a form using javascript to a php script. I want to encode the textarea so it's suitable for sending. Most articles say it is fine, but the third post here: http://www.phpbuilder.com/board/showthread.php?t=10318476 says that it is not the same. I've start using this piece of code; function URLEncode (clearString) { var output = ''; var x = 0; clearString = clearString.toString(); var regex = /(^[a-zA-Z0-9_.]*)/; while (x < clearString.length) { var match = regex.exec(clearString.substr(x)); if (match != null && match.length > 1 && match[1] != '') { output += match[1]; x += match[1].length; } else { if (clearString[x] == ' ') output += '+'; else { var charCode = clearString.charCodeAt(x); var hexVal = charCode.toString(16); output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase(); } x++; } } return output; } Any views? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted September 27, 2009 Share Posted September 27, 2009 Sounds like an ajax thing. But anyway escape() is certainly not the same as urlencode(). Why don't do a simple test containing the & character or send some text containing %20 or something like that. See if something goes wrong on your serverside script. Other then that using urlencode will only really matter if your planning to use the get method even you're using a post method instead you don't really have to bother with it. Quote Link to comment Share on other sites More sharing options...
gevans Posted October 3, 2009 Author Share Posted October 3, 2009 I think I'm just going to stick with the function above, It changes all the characters affected by urlencode() in php. So I am able to urldecode() on the server side. 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.