Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不丢失换行符和空格的情况下将js字符串变量传递给php?_Php_Javascript_String_Variables - Fatal编程技术网

如何在不丢失换行符和空格的情况下将js字符串变量传递给php?

如何在不丢失换行符和空格的情况下将js字符串变量传递给php?,php,javascript,string,variables,Php,Javascript,String,Variables,我有一个javascript变量,它包含一个带有新行和空格的字符串,例如“a\n d”,我希望在不丢失任何空格或新行的情况下将其传递给php。目前我使用的是: my_window = window.open("", "ChemEdit Molfile", "status=1,width=550,height=350"); urlString = "/chemedit/b.php?var=" +r; my_window.location = urlString; 其中r是我传递的字符串

我有一个javascript变量,它包含一个带有新行和空格的字符串,例如“a\n d”,我希望在不丢失任何空格或新行的情况下将其传递给php。目前我使用的是:

my_window = window.open("", "ChemEdit Molfile", "status=1,width=550,height=350");    
urlString = "/chemedit/b.php?var=" +r;
my_window.location = urlString;
其中
r
是我传递的字符串

但是如果我在php中这样做

echo $_GET["var"];
我只在一行上得到它,空格消失了


请提供帮助

我不确定,但请尝试使用
encodeURIComponent
功能:

my_window.location = encodeURIComponent(urlString);

您需要对字符串进行编码,可以使用
encodeURIcomponent()
进行编码。

如果要在url中传递换行符和空格, 您必须遵守url字符串的字符规则, 其中的空格必须表示为“%20”和换行符 “\n”是“%5Cn”。上面的答案可能会这样做 自动,但如果您发现需要手动 编码或调整,这里是所有特殊ascii码的参考 应转换以通过的字符
http头请求:

是否尝试转义r变量?+1,但更像
urlString=“/chemedit/b.php?var=“+encodeURIComponent(r);