繁体   English   中英

HTML表单数据未通过Ajax GET请求传递到php文件

[英]HTML form data is not passed to the php file by ajax GET request

我在创建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()时发送的输入字段值(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");
}
?>

执行此操作时,不会发生任何变化。 而且也没有错误。

您可能在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";
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM