encodeURIComponent用它转码后,php直接接收即可吧!不需要再转了吧!

解决方案 »

  1.   

    将html_entity_decode()换成rawurldecode();
      

  2.   

    htmlspecialchars_decode 试试这个函数
      

  3.   

    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 编码无关了解了这些基础知识以后,我想你应该可以继续了
      

  4.   

    感谢各位大侠的帮助!
    我认真看完后,详细作了测试.
    只是我的输入是一个可编辑的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>
      

  5.   

    建议直接用封装好的jquery来实现ajax的功能需求
      

  6.   

    初学,不懂jquery.
    只是,我这个程序不复杂,应该javascript没问题吧.
      

  7.   


    初学就直接上jquery吧...嘿嘿 研究一下 2小时就通了.
      

  8.   

    把服务端改作<?php
    print_r($_POST);
    exit;运行后可看到
      

  9.   

    郁闷了!
    同样的代码,我的却是下面这个样子:
    会不会和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();
    ?>
      

  10.   

    如果在客户端加上encodeURIComponent转换,又成了这个样子:
      

  11.   

    如果 >'5'
    变成了 >\'5\'
    则表示 magic_quotes_gpc 开关是打开的,自动进行了转义处理
    所以你需要有
    if(get_magic_quotes_gpc()) {
      $_POST['str'] = stripslashes($_POST['str']);
    }
      

  12.   

    感谢xuzuning版主的帮助!
    可以了,感谢感谢!