简体   繁体   English

PHP:简单形式的编码/解码

[英]PHP: simple form encoding/decoding

Probably, this question has been asked before, though, I'll ask it again. 可能以前已经问过这个问题,不过我会再问一次。

Currently, I'm facing a problem with form encoding. 目前,我面临着表单编码的问题。 When posting my form, all spaces are replaced by the "+" character. 发布表单时,所有空格均由“ +”字符代替。 I would like to replace this "+" character by a real space. 我想用一个实际的空格代替这个“ +”字符。

Does someone has a PHP solution for this? 有人为此提供PHP解决方案吗?

Thanks in advance. 提前致谢。

Cheers, Lennart 干杯,伦纳特

Can't reproduce 无法复制

<form>
<input type=text name="a" value="text with spaces">
<input type=submit>
</form>
<?php if (isset($_GET['a'])) echo $_GET['a'] ?>

no spaces at all. 完全没有空格。 What im doing wrong? 我在做什么错?

This shouldn't happen if the browser behaves correctly. 如果浏览器行为正确,则不应发生这种情况。 My assumption would be that a javascript is messing with your data. 我的假设是JavaScript会弄乱您的数据。 Replacing spaces with pluses is done when encoding urls, maybe that will help. 在对网址进行编码时,用加号替换了空格,也许会有所帮助。

You can use firebug to check for any js interference. 您可以使用Firebug检查是否有任何JS干扰。

I'm using AJAX (x = in this case JSON) for handling the form posts etc 我正在使用AJAX(在这种情况下为JSON,x =)处理表单帖子等

Then let's see the code. 然后让我们看一下代码。

Possibly you're doing something like trying to form-encode your data manually before another component also form-encodes it. 可能您正在做类似尝试手动对数据进行表单编码的操作,然后再对另一个组件进行表单编码的数据。 Replacing a space with + is quite standard and expected for form-encoding, but if you accidentally do it twice then you're going to be left with an encoded + at the end of it. +代替空格是很标准的,并且可以用于表单编码,但是如果您不小心重复了两次,那么在它的结尾会留下一个编码的+

If you are using the JavaScript escape function: don't. 如果您使用的是JavaScript escape功能:请不要。 (When you need to URL-encode a form value for inclusion in a parameter, the proper method is encodeURIComponent . escape is a fruity non-standard encoding of its own which you should almost never have any need to use.) (当你需要URL编码形式值包含在一个参数,正确的方法是encodeURIComponentescape是它自己的,你应该几乎从来没有任何需要使用水果的非标准编码。)

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

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