简体   繁体   English

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

[英]URL encoding with Javascript and then decoding with PHP

I am wanting to url encode a string with Javascript, with the relevant part of the code being: 我想用Javascript对字符串进行url编码,其中代码的相关部分是:

this.options[this.selectedIndex].value

or the full line: 或整行:

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

How can I url encode this with JS and then safely decode it with PHP using urldecode ? 如何使用JS对URL进行编码,然后使用urldecode使用PHP安全地对其进行解码?

I had a problem when I wanted to use & in URL. 我想在URL中使用&时遇到问题。 I just solved it using encodeURIComponent() . 我只是使用encodeURIComponent()解决了它。

I used it in onchange function of my dropdown element like this: 我在下拉元素的onchange函数中使用了它,如下所示:

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

It works well in my case. 就我而言,它运作良好。

I tried to use php function urlencode() for this, but I couldn't do that. 我试图为此使用php函数urlencode() ,但我做不到。

You have 3 options: 您有3种选择:

  • escape() will not encode: @*/+ escape()将不会编码: @*/+

  • encodeURI() will not encode: ~!@#$&*()=:/,;?+' encodeURI()将不会编码: ~!@#$&*()=:/,;?+'

  • encodeURIComponent() will not encode: ~!*()' encodeURIComponent()不会编码: ~!*()'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM