使用AJAX的POST请求有点问题。数据似乎没有存储在数据库中。当我使用GET而不是POST时它起作用,但问题是它发送了大量数据,因为它是来自页面上画布的imagedata。我试图打印出发送的数据大小,以确保它在发送之前确实是正确的(大约10500个字符长)。
这是Javascript / AJAX代码:
if(type == "DrawingData" && Drawer)
{
var CanvasData = canvas.toDataURL("image/png");
var url = "DB_Com.php";
var params = "DrawingData="+CanvasData;
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
这是PHP代码:
if(isset($_POST['DrawingData']))
{
$Image = mysql_real_escape_string($_POST['DrawingData']);
$query = "UPDATE BlackboardDrawing SET Data = '$Image'";
mysql_query($query) or die(mysql_error());
}
非常感谢任何帮助。 谢谢!
答案 0 :(得分:0)
您的Ajax看起来很好,但是您需要POST您正在更新的行ID。 :
if(type == "DrawingData" && Drawer)
{
var CanvasData = canvas.toDataURL("image/png");
var url = "DB_Com.php";
var rowid = 'your_row_id';
var params = "DrawingData="+CanvasData+"rowid="+rowid;
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
并更改您的PHP脚本:
if(isset($_POST['DrawingData']))
{
$Image = mysql_real_escape_string($_POST['DrawingData']);
$rowid = (int)$_POST['rowid'];
$query = "UPDATE BlackboardDrawing SET Data = '$Image' WHERE id='$rowid'";
mysql_query($query) or die(mysql_error());
}