简体   繁体   English

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

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

I am getting errors in creating a GET request to send the form data to the PHP file. 我在创建GET请求以将表单数据发送到PHP文件时遇到错误。 What should I do? 我该怎么办?

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 to str4 are the input field values(HTML) I send when calling addDetails(). str1到str4是我在调用addDetails()时发送的输入字段值(HTML)。

My PHP code looks like this 我的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");
}
?>

No change occurs when I'm executing this. 执行此操作时,不会发生任何变化。 And there is no error too. 而且也没有错误。

Your probably getting errors on the php script. 您可能在php脚本上遇到错误。 You can not call the alert() functions in PHP. 您不能在PHP中调用alert()函数。 Use echo instead. 请改用echo。 Then you will see the responses in your browsers console. 然后,您将在浏览器控制台中看到响应。

Your PHP code should look like this: 您的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