I am writing a website that needs to update a users credits so that it adds a javascript value to the existing credits in the database on a button click and I can't seem to find out how to do it (I'm very new to ajax so go easy...)
HTML:
<form method="post">
<input id="depositBtn" type="submit" value="Deposit">
</form>
jQuery
$( "#depositBtn" ).submit( function() {
$.ajax({
url: 'deposit.php',
dataType: 'json',
type: 'post',
data: {total: tot},
success: function(data) {
alert(data);
}
});
});
PHP
$db = new PDO('mysql:host='.$servername.';dbname='.$dbname.';charset=utf8', $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$getCredits = $db->prepare("SELECT credits FROM userinfo WHERE steamid = ? ");
$getCredits->bindParam(1, $steamid, PDO::PARAM_INT);
$credits = $getCredits->fetch(PDO::FETCH_ASSOC);
$allcredits = $credits['credits'];
$bank = $_POST['total'];
$result = array(
'success' => true,
'credits' => $bank+$allcredits
);
echo json_encode($result);