有没有办法可以使用这个JS第二个计数器来写入mysql:
var counter = 0;
setInterval(function () {
++counter;
}, 1000);
我可以将其导出为变量,然后使用该变量写入mysql吗?
我尝试做的是节省用户在页面上的时间。
这甚至可能吗?
答案 0 :(得分:0)
帮助解决你的ajax部分(需要jquery):
$.ajax({
type:'POST',
url: "yourfile.php",
data:{
"foo": filename // to add more variables you add on the spot after filename but remember the last one variable you send shouldn't have a comma
}
});
在接收端(php):
<?php
$filename = $_POST["foo"];
?>
基本上ajax用于将数据发送到php脚本。 Javascript是一种在客户端使用的语言,无法写入MYsql。上面的代码将帮助您将数据发送到您的脚本。一旦通过AJAX收到数据,你的php脚本就会运行
答案 1 :(得分:0)
您需要编写一些后端php来处理来自前端javascript的ajax查询。
您的JS可能如下所示:
function ajaxRequest(data) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", "path/to/back-end.php", true);
xmlHttp.timeout = 1000;
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) { //DONE
//success
console.log("Successfully sent to back-end.php");
}
};
xmlHttp.send(data);
}
您可以参考http://www.w3schools.com/ajax/default.asp了解更多信息。
然后你的php会检索这个POST请求的主体并相应地将它插入到mysql数据库中。