无法使用ajax将完整的数据发送到数据库中

unable to send complete data into database using ajax

本文关键字:数据 数据库 ajax      更新时间:2023-12-03

i m使用以下javascript将数据发送到数据库

var x=document.getElementById("sortable").innerHTML;
    alert(x);
    var http=new XMLHttpRequest();
        http.open("POST", "5.php?field="+x, true);
        http.onreadystatechange = function() 
        {
            if(http.readyState == 4 && http.status == 200) 
            {
                alert(http.responseText);
            }
        }
        http.send();

在php端,我使用以下函数:

$l=$_GET['field'];
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("formdem", $con);
$sql="INSERT INTO rohit(content)VALUES('$l')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "<br/>&nbsp&nbsp&nbsp Your Form is now ready to be filled.....";
$rs= mysql_query("Select * from rohit where content='$l'");
if(mysql_num_rows($rs)!=0)
    {
    while($row = mysql_fetch_array($rs)) {
      echo "<br/>Your unique id is ".$row['formid'];

      }
      }

但该代码并没有将完整的数据发送到数据库中。可能是什么原因。在数据库中,我将字段作为长文本。thnx

由于innerHTML是某种HTML,所以不能简单地添加它来创建URL。你不能这样做:

http.open("POST", "5.php?field="+x, true);

你必须urlEncode x之前:

http.open("POST", "5.php?field="+encodeURIComponent(x), true);