我在创建将表单数据发送到PHP文件的GET请求时遇到错误。我该怎么办?
function addDetails(str1,str2,str3,str4){
var xmlhttp=new XMLHttpRequest();
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("viewblock").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","addDetails.php?a="+str1+"&b="+str2+"&c="+str3+"&d="+str4,true);
xmlhttp.send();
}
str1到str4是我在调用addDetails()时发送的输入字段值(HTML)。
我的PHP代码看起来像这样
$a = $_REQUEST["a"];
$b = $_REQUEST["b"];
$c = $_REQUEST["c"];
$d = $_REQUEST["d"];
$con=new mysqli_connect('localhost','root','');
if(!$con){
die('Connection Error : '.mysqli_error($con));
}
mysqli_select_db($con,"ajax_app");
$sql="INSERT INTO images(title,description,capturedate,image) VALUES ($a,$b,$c,$d)";
if(mysqli_query($con,$sql)){
alert("data added successfully");
}
else{
alert("failed to add");
}
?>
我执行此操作时没有发生任何变化。而且也没有错误。
答案 0 :(得分:0)
您可能在php脚本上遇到错误。你不能在PHP中调用alert()函数。请改用echo。然后,您将在浏览器控制台中看到响应。
您的PHP代码应如下所示:
$a = $_REQUEST["a"];
$b = $_REQUEST["b"];
$c = $_REQUEST["c"];
$d = $_REQUEST["d"];
$con=new mysqli_connect('localhost','root','');
if(!$con){
die('Connection Error : '.mysqli_error($con));
}
mysqli_select_db($con,"ajax_app");
$sql="INSERT INTO images(title,description,capturedate,image) VALUES ($a,$b,$c,$d)";
if(mysqli_query($con,$sql)){
echo "data added successfully";
}
else{
echo "failed to add";
}
?>