Php $\使用AJAX获取(&;)的值
我有三个代码第一个是Php $\使用AJAX获取(&;)的值,php,html,ajax,Php,Html,Ajax,我有三个代码第一个是html: <html> <head> <script type = "text/javascript" src = "AJAX.js"></script> </head> <body> <form method = "GET" > <input type = "text" id ="a" ><br> <input type = "text" i
html
:
<html>
<head>
<script type = "text/javascript" src = "AJAX.js"></script>
</head>
<body>
<form method = "GET" >
<input type = "text" id ="a" ><br>
<input type = "text" id = "b"><br>
<input type = "button" value ="click" onclick = "process()"><br>
<textarea id = "underbutton" ></textarea><br>
</form>
<body>
</html>
现在是php文件:
<?php
echo $_GET['a'].'<br>';
echo $_GET['b'].'<br>';
?>
一切正常,但问题是当我在第一个文本框中键入单词hello
,然后在第二个文本框中键入代码&
并单击按钮时;它必须打印出hello&
但是它会打印
你好
只需
你好
而不必&
我注意到我向php发送的文件是:
file.php?a=hello&b=&
最后一个
&
必须是%26
因此,要打印出&
,我必须发送:
file.php?a=hello&b=%26
如何修复JavaScript中的更改:
- a = document.getElementById("a").value;
- b = document.getElementById("b").value;
+ a = encodeURIComponent(document.getElementById("a").value);
+ b = encodeURIComponent(document.getElementById("b").value);
我认为,使用jQuery.AJAX()可以解决这个问题 您必须对您的值进行URL编码:
xmlHttp.open("GET","file.php?a="+encodeURIComponent(a)+"&b="+encodeURIComponent(b),true);
您需要这样做,因为URL中的参数是在“&”上拆分的,因此类似于:
a=你好
&
b=
&
*(这里可能是第三个参数)*要在URL中编码符号(&),您可以使用&代码>。用它替换时会发生什么情况?使用encodeURIComponent()
你不能,真的。&
在查询字符串中有特殊的含义-它是参数分隔符。如果您想将其作为值包含,则需要对其进行编码。问题不在于HTML中的(&E)。
xmlHttp.open("GET","file.php?a="+encodeURIComponent(a)+"&b="+encodeURIComponent(b),true);
xmlHttp.open("GET","file.php?a="+encodeURIComponent(a)+"&b="+encodeURIComponent(b),true);