admin管理员组文章数量:1332345
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 +urlencode+w3schools
.
EDIT
Sorry, I forgot to mention, it's not form submission but a ajax request.
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.
Share Improve this question edited Feb 25, 2010 at 13:57 user266307 asked Feb 25, 2010 at 7:42 user266307user266307 771 silver badge7 bronze badges3 Answers
Reset to default 3Use encodeURIComponent(uri)
(for encoding) and decodeURIComponent(uri)
for decoding,
E.g (encoding).
var uri="http://w3schools./my test.asp?name=ståle&car=saab";
document.write(encodeURIComponent(uri));
Output
http%3A%2F%2Fw3schools.%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab
Decoding is left for the reader. :-)
Source: http://www.w3schools./jsref/jsref_encodeURIComponent.asp
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 ment. Try this.
<form id="form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"
onsubmit="this.via_js.value=this.via_plain_form.value;">
<input type="hidden" name="via_js"/>
<input type="text" name="via_plain_form" value="Paste you url here"/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if (isset($_POST['submit'])) {
var_export($_POST);
}
?>
For http://www.google.co.in/search?q=javascript+urlencode+w3schools
, it outputs
array (
'via_js' => 'http://www.google.co.in/search?q=javascript+urlencode+w3schools',
'via_plain_form' => 'http://www.google.co.in/search?q=javascript+urlencode+w3schools',
'submit' => 'Submit',
)
As for PHP, it's urlencode()
and urldecode()
.
本文标签: How to encode URL in JavaScript and PHPStack Overflow
版权声明:本文标题:How to encode URL in JavaScript and PHP? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742327350a2453989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论