当用户填写调查问卷时,在调用Javascript函数的末尾,将设置三个变量。现在,我要使用这三个变量并将它们填充到通过XAMPP在本地主机上运行的MySQL数据库中。我的项目在XAMPP的htdocs文件夹中。我已经尝试过使用jQuery进行AJAX方法,并且也尝试过在JS中使用此功能:
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
data = "uni_id="+variable1+"&additiv="+variable2+"&konventionell="+variable3;
xmlhttp.open("POST","speicherung.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(data);
}
我的speicherung.php内部看起来像这样:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "survey_data";
if(isset($_POST)){
$uni_id = $_POST['uni_id'];
$additiv = $_POST['additiv'];
$konventionell = $_POST['konventionell'];}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO result_data (uni_id,additiv,konventionell,data_js)
VALUES ('$uni_id', '$additiv', '$konventionell', '0')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
不幸的是,当我执行JS时,它没有在MySQL表中插入任何内容。我该怎么做才能正确地将JS变量移交给我的数据库,以便以后可以在任何给定时间从html访问它们?
谢谢!