我使用以下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/>    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'];
}
}
但此代码未将完整数据发送到数据库。可能是什么原因。在数据库中我把字段作为longtext。 日Thnx
答案 0 :(得分:1)
由于您的innerHTML是一些HTML,因此无法简单地添加它来创建URL。你不能这样做:
http.open("POST", "5.php?field="+x, true);
你必须在之前urlEncode x:
http.open("POST", "5.php?field="+encodeURIComponent(x), true);