使用Javascript进行URL编码,然后使用PHP解码

问题描述 投票:0回答:2

我想要用Javascript对字符串进行url编码,代码的相关部分是:

this.options[this.selectedIndex].value

或者全线:

<select name="category" onChange="javascript:document.location.href='<? echo '/' . $state . '/dir/'; ?>' + this.options[this.selectedIndex].value;">

我如何用JS对其进行url编码,然后使用urldecode安全地解码它?

php javascript urlencode url-encoding urldecode
2个回答
0
投票

当我想在URL中使用&时,我遇到了问题。我刚用encodeURIComponent()解决了它。

我在我的dropdown元素的onchange函数中使用它,如下所示:

onchange="window.location='test.php?c='+encodeURIComponent(this.value)"

它适用于我的情况。

我尝试使用php函数urlencode(),但我无法做到这一点。


-1
投票

你有3个选择:

  • escape()不会编码:@*/+
  • encodeURI()不会编码:~!@#$&*()=:/,;?+'
  • encodeURIComponent()不会编码:~!*()'
© www.soinside.com 2019 - 2024. All rights reserved.