Question

Im not quite sure how to go about putting an ajax function inside of my javascript timer so that everytime it restarts then itll add 1 of the item into the database. ive been looking and i found this: http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php but im not quite sure how to implement it into the code, so if you could help me itd be appreciated.

Heres 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>
Était-ce utile?

La solution

try including jquery and put $.post('path/to/file.php', {param1: value1}); in your doMining() function

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top