【问题标题】:Passing variable via query string truncates plus symbol通过查询字符串截断加号传递变量
【发布时间】:2018-08-02 20:56:09
【问题描述】:

我想知道如何在 Javascript 中将变量传递为 +4,然后在 php 中将其检索为 +4

无论我做什么,每当我存储 +4" 并验证它是否被存储时,当我检索它时,+ 消失了,我得到的只是数字(即4)。

这是我得到的:

file.js

var testString = "+4";
console.log(testString);
window.location.href = "file.php?testString=" + testString;

输出:+4

file.php

$testString = $_GET["testString"];
echo $testString;

输出:4

我知道我总是可以附加“+”,但这似乎是一个不必要的额外过程;我宁愿正确地传递变量。

【问题讨论】:

    标签: javascript php mysql


    【解决方案1】:

    你需要encode你的参数:

    var testString = "+4";
    console.log(testString);
    window.location.href = "file.php?testString=" + encodeURIComponent(testString);
    

    有一些问题/资源可以帮助您理解为什么需要这样做:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      相关资源
      最近更新 更多