Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.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
Javascript 逃逸及;一串?_Javascript_Php_Mysql - Fatal编程技术网

Javascript 逃逸及;一串?

Javascript 逃逸及;一串?,javascript,php,mysql,Javascript,Php,Mysql,人们对我上一个问题的建议似乎没有什么帮助。因此,我想在这里用完整的代码再次发布: mysql_depricated.php: <script> function fun() { var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest();

人们对我上一个问题的建议似乎没有什么帮助。因此,我想在这里用完整的代码再次发布: mysql_depricated.php:

<script>
 function fun()
 {
 var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

   var name = document.getElementById("name").value;
   var url = "mysql_depricated2.php";
   var param = "name=" + name;

   ajaxRequest.open("POST", url, true);

//Send the proper header information along with the request
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", param.length);
ajaxRequest.setRequestHeader("Connection", "close");

    ajaxRequest.send(param); 
 }
</script>

 <input type="text" id="name">
 <input type="button" value="ajax&" onclick="fun()">

函数fun()
{
var ajaxRequest;//使Ajax成为可能的变量!
试一试{
//Opera 8.0+、Firefox、Safari
ajaxRequest=新的XMLHttpRequest();
}捕获(e){
//Internet Explorer浏览器
试一试{
ajaxRequest=newActiveXObject(“Msxml2.XMLHTTP”);
}捕获(e){
试一试{
ajaxRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(e){
//出了点问题
警告(“你的浏览器坏了!”);
返回false;
}
}
}
var name=document.getElementById(“name”).value;
var url=“mysql\u depricated2.php”;
var param=“name=”+name;
打开(“POST”,url,true);
//随请求一起发送正确的标头信息
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
ajaxRequest.setRequestHeader(“内容长度”,参数长度);
setRequestHeader(“连接”、“关闭”);
ajaxRequest.send(参数);
}
mysql_去润滑2:

<?php
      $host="localhost"; // Host name
      $username="root"; // Mysql username
      $password=""; // Mysql password
      $db_name="example"; // Database name

   // Connect to server and select databse.
   $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
   mysql_select_db("$db_name")or die("cannot select DB");


 $content = $_POST['name'];
 $content=mysql_real_escape_string($content);

 $sql = "insert into e values('$content')";
 mysql_query($sql);
?>

替换以下行:

ajaxRequest.send(param);
对于这一个:

ajaxRequest.send( encodeURIComponent(param) );

因此,您将
&
特殊字符编码为其url编码形式,即
%26

替换以下行:

ajaxRequest.send(param);
对于这一个:

ajaxRequest.send( encodeURIComponent(param) );

因此,您将
&
特殊字符编码为其url编码形式,即
%26

我应该如何解码才能成功地将其插入数据库@Nelsony您不需要做任何特殊的解码工作,PHP会自动为您进行解码。如果我使用encodeURIComponent和echo$content,它只会显示一个空字符串。请尝试在分配
name
ie时使用encodeURIComponent。
var param=“name=”+encodeURIComponent(name)
否则您的ajax代码可能会有问题,因为encodeURIComponent运行良好,您可以在感谢@nelson提供的宝贵帮助中看到这一点。我试着附和结果,结果非常令人满意。也就是说,如果我输入#124,那么PHP会回显#124,或者对于输入&127,PHP会精确打印&127,但是如果我输入say{,那么PHP只会输入{.我不知道为什么…..再次混淆了我应该怎么做才能将其解码以便成功插入数据库?@Nelsony您不需要做任何特殊的解码,PHP会自动为您执行此操作。如果我使用encodeURIComponent和echo$content,它只显示一个空字符串,请在分配时尝试使用encodeURIComponent<代码>名称
ie.
var param=“name=”+encodeURIComponent(名称)
否则,您的ajax代码可能会有问题,因为encodeURIComponent运行良好,您可以在感谢@nelson的宝贵帮助中看到。我尝试回显结果,结果非常令人满意。例如,如果我输入#124,则PHP回显#124;如果输入,则PHP精确打印,但如果我输入say{,则PHP回显只需输入{。我不知道为什么…..再次混淆您自己问题的副本:。如果您想提供进一步信息,请编辑现有问题,不要再次提问。您自己问题的副本:。如果您想提供进一步信息,请编辑现有问题,请不要再次提问。