Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include this function in your javascript and to be use to send won to backend
- function update(won){
- if (won != '' || won != 'undefined') {
- $.ajax({
- url: "gameupdate.php",
- type: "POST",
- data: {"updatewon" : won},
- success: function (data) {
- if ($.trim(data) == 'success') {
- alert('updated');
- } else {
- alert('something goes wrong');
- }
- }
- });
- }
- }
- // when you want to update won
- // update(won); <===
- gameupdate.php
- #########################
- <?php
- if (!emtpy($_POST['updatewon'])) {
- // validate $updatewon is within 0 to 100
- $updatewon = (in_array($_POST['updatewon'], range(0, 100)))? $_POST['updatewon'] : 0;
- if(!$link = mysqli_connect("localhost", "root", "....", "db")) {
- die ("No connection: ".mysqli_connect_error());
- }
- $res = mysqli_query($link, "select balance from registrations where username='{$_SESSION[username]}'");
- // $Balance = $res;
- if (mysqli_num_rows($res) === 1) {
- // insert newblc to database;
- if (mysqli_query($link, "UPDATE registrations SET balance = balance + {$updatewon} WHERE username = '{$_SESSION[username]}'")) {
- echo 'success'; // this output will be send to DOM as data response
- exit;
- } else {
- echo 'upddate failure';
- exit;
- }
- } else {
- echo 'no balance was found';
- exit;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement