I want to send a URL in a POST request in a variable called surl
. How should I encode it in JavaScript and decode it in PHP? For example, the value of surl
could be http://www.google.co.in/search?q=javascript+urlencode+w3schools
.
EDIT
Sorry, I forgot to mention, it's not form submission but a ajax request.
As for PHP, it's
urlencode()
andurldecode()
.You don't need to to anything. Send it as is. Browser and PHP will do all escaping and unescaping for you (if you use
form.surl.value = surl; form.submit()
and$_POST['surl']
). Or you can just use the plain form without any JavaScript (if it fulfills your needs).Replying henasraf's comment. Try this.
For
http://www.google.co.in/search?q=javascript+urlencode+w3schools
, it outputsUse
encodeURIComponent(uri)
(for encoding) anddecodeURIComponent(uri)
for decoding,E.g (encoding).
Output
Decoding is left for the reader. :-)
Source: http://www.w3schools.com/jsref/jsref_encodeURIComponent.asp