I am totally new to AJAX so I don't even know where to start. I've tried delaying the submission of the form with jQuery but then the script doesn't even recieve the POST request.
My index.php
<?php
include 'includes.php';
$user_mining_level = 16;
$user_id = 1;
if(isset($_POST['mine'])) {
if($user_mining_level == 1) {
$random_ore = rand(1, 2);
}
if($user_mining_level > 15) {
$random_ore = rand(1, 3);
}
$random = rand(1, 5);
$xp_gained = $random * $ore_xp["$random_ore"];
$random_ore_name = $ore["$random_ore"];
$stmt = $conn->prepare('UPDATE invy_ores SET `' . $random_ore_name . '` = `' . $random_ore_name. '` + ' . $random . ' WHERE user_id = :user_id');
$stmt->bindValue(":user_id", $user_id, PDO::PARAM_INT);
$stmt->execute();
$result = 'You\'ve managed to mine ' . $random . ' ' . $ore["$random_ore"] . ' and gained ' . $xp_gained . ' EXP!';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>RuneScape Click Game</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<div id="result"><?php echo $result; ?></div>
<form action="index.php" method="POST" class="gather">
<input type="submit" name="mine" value="Mine" id="ajax_bt"/>
</form>
</body>
For this to work ideally I'd need the form submission to be delayed by about 3 seconds, and the data from the $result
variable viewable within the <div id="result"></div>
element. I've been searching for hours on how I could do this and I just cannot seem to find an answer. Could someone show me an example or steer me in the right direction?
EDIT:
This seems to inch towards what I want to do but the form is not functioning after the delay.
$(document).ready(function() {
$(".gather").submit(function (e) {
e.preventDefault();
var form = this;
$("<img src='loader.gif' alt='Loading...' />").appendTo("#result");
$("result").hide();
setInterval(function () {
form.submit();
}, 1000); // in milliseconds
});
});