我在创建GET请求以将表单数据发送到PHP文件时遇到错误。我该怎么办?
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()时发送的输入字段值。
我的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");
}
?>
当我执行这段代码时,没有发生任何变化。而且也没有错误。
发布于 2018-05-19 16:16:41
你可能会在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";
}
?>
https://stackoverflow.com/questions/50423141
复制相似问题