不知道如何在 Javascript 中执行 ajax 函数来访问 mysql
我不太确定如何将 ajax 函数放入我的 Javascript 计时器中,以便每次重新启动时都会将其中一项添加到数据库中。我一直在寻找,发现了这个:http://www.tizag。 com/ajaxTutorial/ajax-mysql-database.php 但我不太确定如何将其实现到代码中,所以如果您能帮助我,我将不胜感激。
到目前为止,这是我的代码:
<head>
<script type="text/javascript">
var c=10;
var mineCount = 0;
var t;
var timer_is_on=0;
function timedCount() {
document.getElementById('txt').value = c;
c = c - 1;
if (c <= -1) {
mineCount++;
var _message = "You have mined " + mineCount + " iron ore" + (((mineCount > 1) ? "s" : "") + "!");
document.getElementById('message').innerHTML = _message;
startover();
}
}
function startover() {
c = 10;
clearTimeout(t);
timer_is_on=0;
doMining();
}
function doMining() {
if (!timer_is_on) {
timer_is_on = true;
t = setInterval(function () {
timedCount();
}, 1000);
}
}
</script>
<SPAN STYLE="float:left">
<form>
<input type="button" value="Mining" onClick="doMining()">
<input type="text" id="txt">
</form>
</SPAN>
<html>
<center>
<div id='message'></div>
I'm not quite sure how to go about putting an ajax function inside of my Javascript timer so that every time it restarts then it'll add one of the item into the database. I've been looking and I found this: http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php but I'm not quite sure how to implement it into the code, so if you could help me it'd be appreciated.
Here's my code so far:
<head>
<script type="text/javascript">
var c=10;
var mineCount = 0;
var t;
var timer_is_on=0;
function timedCount() {
document.getElementById('txt').value = c;
c = c - 1;
if (c <= -1) {
mineCount++;
var _message = "You have mined " + mineCount + " iron ore" + (((mineCount > 1) ? "s" : "") + "!");
document.getElementById('message').innerHTML = _message;
startover();
}
}
function startover() {
c = 10;
clearTimeout(t);
timer_is_on=0;
doMining();
}
function doMining() {
if (!timer_is_on) {
timer_is_on = true;
t = setInterval(function () {
timedCount();
}, 1000);
}
}
</script>
<SPAN STYLE="float:left">
<form>
<input type="button" value="Mining" onClick="doMining()">
<input type="text" id="txt">
</form>
</SPAN>
<html>
<center>
<div id='message'></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试包含 jquery 并放入
$.post('path/to/file.php', {param1: value1} );
在您的doMining()
函数中try including jquery and put
$.post('path/to/file.php', {param1: value1});
in yourdoMining()
functionAJAX 的想法是,我们将使用任何脚本语言(如 Javascript)从客户端向服务器发送同步/异步请求,并在服务器端处理中我们将执行所需的功能并根据需要发送响应。
我对 PHP 不太确定,因为我通常在 dotnet 中工作。
因此,在 PHP 中,我希望有某种方法可以创建 Web 服务或创建网页,在该页面或服务中通过使用查询字符串或帖子获取适当的参数,然后调用更新数据库所需的功能。然后你可以编写响应,将响应发送回 JS 客户端。 JS 可以解析响应并可以根据需要更新 UI。
一些参考:
在 Javascript 中使用 XmlHttp
Idea of AJAX is, we will send sync/async request to server from the client side by using any scripting language like Javascript, and in server side processing we will do the desired functionality and send response as required.
I am not sure about PHP, as i generally work in dotnet.
So in PHP i hope there would be some way to create a web service or to create a web page, in that Page or Service get the appropriate parameters by using query string or post and then call the desired functionality of updating the DB. Then you can write in response to send the response back to JS client. JS can parse the reponse and can update UI as required.
Some references:
Using XmlHttp in Javascript