【问题标题】:Symbol "+" disappear inside input text when post form发布表单时,符号“+”在输入文本中消失
【发布时间】:2012-06-17 06:30:23
【问题描述】:

我正在使用 Zend 框架。我有一个包含一些字段的表单,它们都是type="text",包括一个名为“电话”的输入。提交时,我使用 Ajax 将数据发送到控制器。那么这是我的问题,如果我输入 + 符号,例如: +34-666666666 我收到的数据是34-666666666+ 符号变成一个空格。这个问题只发生在+,我尝试了所有的符号,没有问题。我快疯了,我在 Google 中没有找到任何解决方案。

【问题讨论】:

  • 您是如何提交数据的?

标签: php ajax forms zend-framework textinput


【解决方案1】:

您需要按照here 的说明对 GET/POST 数据进行编码。 JavaScript encodeURIComponent 函数可以稍加改动*:

对于 application/x-www-form-urlencoded (POST),根据规范,空格是 被 '+' 取代,因此可能希望遵循一个 encodeURIComponent 将“%20”替换为“+”。

"&phone=" + encodeURIComponent("+34-666666666").replace(/%20/, "+");      // "&phone=%2B34-666666666"
"&phone=" + encodeURIComponent("+34-666666666 x123").replace(/%20/, "+"); // "&phone=%2B34-666666666+x123"

*注意:URL 中+ 字符的处理方式取决于它是用于路径组件还是查询字符串中:

  1. http://example.com/page+1 -- 文件名page+1
  2. http://example.com/page%201 -- 文件名page 1
  3. http://example.com/?file=page+1 -- 查询字符串参数file=page 1
  4. http://example.com/?file=page%201 -- 查询字符串参数file=page 1

【讨论】:

    【解决方案2】:

    + 符号在 URL 中用于表示空格。您的 ajax 提交可能正在执行 GET 请求,并且 URL 字符串中的 + 正在转换。 在使用 encodeURIComponent() 提交 ajax 请求之前,请通过 javascript 清理您的输入。

    您可能还会发现这个问题很有用:Plus character in URL transformed to space on a linux box

    本题讨论encodeURIComponentHow to encode a URL in Javascript?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      相关资源
      最近更新 更多