【问题标题】:How to deal with spaces in a PHP request如何处理 PHP 请求中的空格
【发布时间】:2014-03-27 04:26:19
【问题描述】:

假设您有两个按钮。他们的 ID 是:

bob
bobs burgers

您使用 jquery 在 div 元素中加载 php 页面,如下所示:

var teamSelect = $(this).attr('id');
$("#loadTables").load("php/leagueTable.php?team="+teamSelect);

现在 bob 可以正常运行了,但是 bob burgers 有一个空间并且无法运行。地址最终是

"php/leagueTable.php?team=bobs"

我无法控制按钮 ID 中有空格这一事实。这些按钮是从数据库生成的,这些名称将始终包含空格。我该如何处理?

【问题讨论】:

标签: php jquery spaces


【解决方案1】:

你需要使用encodeURIComponent对url进行编码

$("#loadTables").load("php/leagueTable.php?team="+encodeURIComponent(teamSelect));

encodeURIComponent() 方法对统一资源标识符进行编码 (URI) 组件,通过将某些字符的每个实例替换为 一个、两个、三个或四个表示 UTF-8 的转义序列 字符的编码(将只有四个转义序列 由两个“代理”字符组成的字符),reference

【讨论】:

  • 就这样工作。谢谢。
【解决方案2】:

如果这些值不包含引号,则用单引号将这些值括起来。

喜欢:

'bob'
'bob burgers'

这样当它传递给 url 时就会是

php/leagueTable.php?team='bob'
php/leagueTable.php?team='bob burgers'

然后您可以在之后使用trim() 将它们剥离。

【讨论】:

    【解决方案3】:

    根据w3.org

    The value must be unique amongst all the IDs in the element's home
    subtree and must contain at least one character. The value must not 
    contain any space characters.
    

    你可以找到更多here

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      相关资源
      最近更新 更多