如何通过javascript向php服务器传送字符串 encodeURIComponent用它转码后,php直接接收即可吧!不需要再转了吧! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将html_entity_decode()换成rawurldecode(); htmlspecialchars_decode 试试这个函数 1、你的总是要输入在文本控件中的,无论是表单提交还是ajax发送,均不需要做处理2、在用ajax发送时,如果页面不是 utf-8 的,且发送的串中可能有中文。则应考虑在设计ajax部件时加入 encodeURIComponent 做 url 编码,服务端需做字符集转换3、服务端在收到数据后,应根据 get_magic_quotes_gpc 的状态进行去转义操作(stripslashes)php5.3以后 magic_quotes_gpc 开关默认关闭和取消,已不再需要这一步了4、encodeURIComponent 是按utf-8字符集做 url 编码处理,即无论页面是什么字符集的,服务端得到的都是 utf-8 的数据5、html_entity_decode 是 htmlentities 的逆函数,与 url 编码无关了解了这些基础知识以后,我想你应该可以继续了 感谢各位大侠的帮助!我认真看完后,详细作了测试.只是我的输入是一个可编辑的DIV,只是,在测试时,也换为文本框试过.不作任何转换,如果只是在输入框输入:select * from tablename这样的查询没问题,如果是:select * from tablename where id>5,就出错了.在发送时加encodeURIComponent函数,服务器用html_entity_decode后,可以select * from tablename where id>5正常出结果.当初用这个encodeURIComponent时,只是找啊找,找到这个试了试,只是在php下却没有相应的解码函数,就用的html_entity_decode试.下面是我的测试代码:客户端<!DOCTYPE html><html><head> <title>测试</title> <meta http-equiv="Content-Type" content="text/html"; charset="utf-8"> <script> function handleStateChange(){ if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ document.getElementById("md").innerHTML=xmlHttp.responseText; } } } function createXMLHttpRequest(){ try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (oc) { xmlHttp=null; } } if (!xmlHttp && typeof XMLHttpRequest !="undefined"){ xmlHttp=new XMLHttpRequest(); } return xmlHttp; } function sendstr(){ var xmlHttp=createXMLHttpRequest(); //var str="str="+document.getElementById("sqlstr").innerHTML; //var str="str="+document.getElementById("sqlstr").value; //var str="str="+encodeURIComponent(document.getElementById("sqlstr").value); var str="str="+encodeURIComponent(document.getElementById("sqlstr").innerHTML); var url="t2_1get.php"; xmlHttp.open("POST",url,true); xmlHttp.onreadystatechange=handleStateChange; xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); xmlHttp.send(str); } </script> </head><body><!-- <input type="text" id="sqlstr" />--> <div id="sqlstr" contenteditable="true" style="border:1px solid #ccf;width:200px;height:20px;"></div> <input type="submit" value="query" onclick="sendstr()"/> <div id="md"></div></body></html>服务端<!DOCTYPE html><html><head> <title>测试get</title> <meta http-equiv="Content-Type" content="text/html"; charset="utf-8"></head><body><?php $mysqli=new mysqli("localhost","minhaouser","24994012499401",'test'); if ($mysqli->connect_error){ printf("连接失败:%s\n",$mysqli->connect_error); exit(); } $mysqli->query("set names 'utf8'"); //$sqlstr=$_POST['str']; $sqlstr=html_entity_decode($_POST['str'],ENT_COMPAT,'UTF-8'); //$sqlstr=html_entity_decode($_POST['str']); echo "sqlstr=".$sqlstr."<br />"; $res=$mysqli->query($sqlstr); if (!$res){ echo $mysqli->error; exit; } $finfo=$res->fetch_fields(); echo "<table border='1'><tr>"; foreach($finfo as $val){ echo "<td>".$val->name."</td>"; } echo "</tr>"; while($row=$res->fetch_row()){ echo "<tr>"; for ($i=0;$i<$res->field_count;$i++){ echo "<td>".$row[$i]."</td>"; } echo "</tr>"; } echo "</table>";?></body></html> 建议直接用封装好的jquery来实现ajax的功能需求 初学,不懂jquery.只是,我这个程序不复杂,应该javascript没问题吧. 初学就直接上jquery吧...嘿嘿 研究一下 2小时就通了. 把服务端改作<?phpprint_r($_POST);exit;运行后可看到 郁闷了!同样的代码,我的却是下面这个样子:会不会和apache及php的版本有问题,或者有什么设置不对.我的apache是2.2.4,php是5.2.3再附测试代码:<!DOCTYPE html><html><head> <title>测试</title> <meta http-equiv="Content-Type" content="text/html"; charset="utf-8"> <script> function handleStateChange(){ if(xmlHttp.readyState==4){ if(xmlHttp.status==200){ document.getElementById("md").innerHTML=xmlHttp.responseText; } } } function createXMLHttpRequest(){ try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (oc) { xmlHttp=null; } } if (!xmlHttp && typeof XMLHttpRequest !="undefined"){ xmlHttp=new XMLHttpRequest(); } return xmlHttp; } function sendstr(){ var xmlHttp=createXMLHttpRequest(); var str="str="+document.getElementById("sqlstr").innerHTML; var url="t2_1get.php"; xmlHttp.open("POST",url,true); xmlHttp.onreadystatechange=handleStateChange; xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); xmlHttp.send(str); } </script> </head><body> <div id="sqlstr" contenteditable="true" style="border:1px solid #ccf;width:200px;height:20px;"></div> <input type="submit" value="query" onclick="sendstr()"/> <div id="md"></div></body></html>t2_1get.php:<?php print_r($_POST); exit();?> 如果在客户端加上encodeURIComponent转换,又成了这个样子: 如果 >'5'变成了 >\'5\'则表示 magic_quotes_gpc 开关是打开的,自动进行了转义处理所以你需要有if(get_magic_quotes_gpc()) { $_POST['str'] = stripslashes($_POST['str']);} 感谢xuzuning版主的帮助!可以了,感谢感谢! 怎么在页面上固定位置显示数据库的值 php+mysql读取数据问题 php 中的xml 100 分 留言板文本处理 PHP如何获取一张数码照片中的基本信息,譬如相机信息之类的资料? 幫我找個問題,謝謝 (请教高手)用Apache+Php如何支持Oracle啊? 有51.net空间的朋友来看看,Session为什么不能用 我想找一个支持 PHP+Mysql 的虚拟主机,数据库空间较大的,想放vBulletin和PHPNUKE, 请大家推荐一下。 紧急求救!!如何在网页间传递变量参数(比如登录后的用户名)?? 读取session中数组问题 php取出数组中部分字符并根据字符排序
2、在用ajax发送时,如果页面不是 utf-8 的,且发送的串中可能有中文。则应考虑在设计ajax部件时加入 encodeURIComponent 做 url 编码,服务端需做字符集转换
3、服务端在收到数据后,应根据 get_magic_quotes_gpc 的状态进行去转义操作(stripslashes)
php5.3以后 magic_quotes_gpc 开关默认关闭和取消,已不再需要这一步了
4、encodeURIComponent 是按utf-8字符集做 url 编码处理,即无论页面是什么字符集的,服务端得到的都是 utf-8 的数据
5、html_entity_decode 是 htmlentities 的逆函数,与 url 编码无关了解了这些基础知识以后,我想你应该可以继续了
我认真看完后,详细作了测试.
只是我的输入是一个可编辑的DIV,只是,在测试时,也换为文本框试过.
不作任何转换,如果只是在输入框输入:select * from tablename这样的查询没问题,如果是:select * from tablename where id>5,就出错了.
在发送时加encodeURIComponent函数,服务器用html_entity_decode后,可以select * from tablename where id>5正常出结果.
当初用这个encodeURIComponent时,只是找啊找,找到这个试了试,只是在php下却没有相应的解码函数,就用的html_entity_decode试.下面是我的测试代码:
客户端
<!DOCTYPE html>
<html>
<head>
<title>测试</title>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<script>
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
document.getElementById("md").innerHTML=xmlHttp.responseText;
}
}
}
function createXMLHttpRequest(){
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (oc) {
xmlHttp=null;
}
}
if (!xmlHttp && typeof XMLHttpRequest !="undefined"){
xmlHttp=new XMLHttpRequest();
}
return xmlHttp;
}
function sendstr(){
var xmlHttp=createXMLHttpRequest();
//var str="str="+document.getElementById("sqlstr").innerHTML;
//var str="str="+document.getElementById("sqlstr").value;
//var str="str="+encodeURIComponent(document.getElementById("sqlstr").value);
var str="str="+encodeURIComponent(document.getElementById("sqlstr").innerHTML);
var url="t2_1get.php";
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(str);
}
</script>
</head>
<body>
<!-- <input type="text" id="sqlstr" />-->
<div id="sqlstr" contenteditable="true" style="border:1px solid #ccf;width:200px;height:20px;"></div>
<input type="submit" value="query" onclick="sendstr()"/>
<div id="md"></div>
</body>
</html>服务端
<!DOCTYPE html>
<html>
<head>
<title>测试get</title>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
</head>
<body>
<?php
$mysqli=new mysqli("localhost","minhaouser","24994012499401",'test');
if ($mysqli->connect_error){
printf("连接失败:%s\n",$mysqli->connect_error);
exit();
}
$mysqli->query("set names 'utf8'");
//$sqlstr=$_POST['str'];
$sqlstr=html_entity_decode($_POST['str'],ENT_COMPAT,'UTF-8');
//$sqlstr=html_entity_decode($_POST['str']);
echo "sqlstr=".$sqlstr."<br />";
$res=$mysqli->query($sqlstr);
if (!$res){
echo $mysqli->error;
exit;
}
$finfo=$res->fetch_fields();
echo "<table border='1'><tr>";
foreach($finfo as $val){
echo "<td>".$val->name."</td>";
}
echo "</tr>";
while($row=$res->fetch_row()){
echo "<tr>";
for ($i=0;$i<$res->field_count;$i++){
echo "<td>".$row[$i]."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
只是,我这个程序不复杂,应该javascript没问题吧.
初学就直接上jquery吧...嘿嘿 研究一下 2小时就通了.
print_r($_POST);
exit;运行后可看到
同样的代码,我的却是下面这个样子:
会不会和apache及php的版本有问题,或者有什么设置不对.
我的apache是2.2.4,
php是5.2.3再附测试代码:
<!DOCTYPE html>
<html>
<head>
<title>测试</title>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<script>
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
document.getElementById("md").innerHTML=xmlHttp.responseText;
}
}
}
function createXMLHttpRequest(){
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (oc) {
xmlHttp=null;
}
}
if (!xmlHttp && typeof XMLHttpRequest !="undefined"){
xmlHttp=new XMLHttpRequest();
}
return xmlHttp;
}
function sendstr(){
var xmlHttp=createXMLHttpRequest();
var str="str="+document.getElementById("sqlstr").innerHTML;
var url="t2_1get.php";
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(str);
}
</script>
</head>
<body>
<div id="sqlstr" contenteditable="true" style="border:1px solid #ccf;width:200px;height:20px;"></div>
<input type="submit" value="query" onclick="sendstr()"/>
<div id="md"></div>
</body>
</html>t2_1get.php:
<?php
print_r($_POST);
exit();
?>
变成了 >\'5\'
则表示 magic_quotes_gpc 开关是打开的,自动进行了转义处理
所以你需要有
if(get_magic_quotes_gpc()) {
$_POST['str'] = stripslashes($_POST['str']);
}
可以了,感谢感谢!