0

I want to integrate a Java script Slot Machine game into my script.

You can see demo here ; http://odhyan.com/slot/

And also git hub is here ; https://github.com/odhyan/slot you can see all JS files here.

I created a Point Coloumn in User Table that people can play the game with this Point.

I think this JS Function in slot.js checking if user won the game or lose.

function printResult() {
        var res;
        if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
            res = "You Win!";
        } else {
            res = "You Lose";
        }
        $('#result').html(res);
    }

enter image description here

So i want to add +100 Point if user won the bet.

I made this PHP codes Uptading points For userid "1".

<?php

mysql_connect ("localhost","username","password") or die (mysql_error());
mysql_select_db('slot_machine');
$pointsql = mysql_query("SELECT * FROM user WHERE userid = 1");
while ($row = mysql_fetch_array($pointsql))
{
$row['point'] +=100;
$addpoint =  mysql_query("UPDATE user SET point = '{$row['point']}' WHERE userid = 1");
}

?>

So how can i call or excute this PHP Codes in JavaScript function if user Win?

1
  • 5
    There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. Commented Aug 3, 2013 at 4:21

3 Answers 3

2

You'll need to trigger a network request from your javascript code to execute your php script server side. Using jQuery's $.ajax() function is an extremely common way to do this abstracting away various browser differences.

function printResult() {
    var res;
    if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
        res = "You Win!";
        // Assign handlers immediately after making the request,
        // and remember the jqxhr object for this request
        var jqxhr = $.ajax( "path/to/your.php" )
                       .done(function() { alert("success"); })
                       .fail(function() { alert("error"); })
                       .always(function() { alert("complete"); });
    } else {
        res = "You Lose";
    }
    $('#result').html(res);
}
Sign up to request clarification or add additional context in comments.

8 Comments

i think jQuery is already icluded with this code ; <script src="ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
But when try with your codes, it gives JS Alert " error " . i.imgur.com/Jie9cQ9.jpg
You are correct! (Though it's an older version... might be a good idea to update it as it doesn't look like a huge dependent code base here). Thanks for noting this @JacKy.
hmm... did you change "path/to/your.php" to wherever your .php file is hosted? (Otherwise the "error" alert is exactly what I'd expect ;-) ) You may not need the updated jQuery just for this... not sure what might be causing the freeze without looking at the rest of your app in more detail
I'm running this on wamp server at my home. So i put .php file into same folder that .js and index file of game. And edit like this ajax( "addpoint.php" ) and try like this "/addpoint.php" but still getting error
|
1

You can use jQuery's $.post() function to trigger an asynchronous request to your PHP file.

function printResult() {
    var res;
    if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
        res = "You Win!";
        // Here's the line you need.
        $.post('score.php', {userid: 1}, function(data) {
            alert("Score saved.");
        });
    } else {
        res = "You Lose";
    }
    $('#result').html(res);
}

This will send POST data to score.php, or whichever file you want to send the data to. The PHP file can then access the userid sent to it by checking the value of $_POST['userid'].

As mentioned in the documentation, $.post() is a shortcut for jQuery's $.ajax() function that is simplified and has some of its options pre-set. The third argument in $.post() is a callback function, and the variable data will contain whatever is echoed out or printed from score.php by the time it's done executing. So, you could use alert(data) instead, to see what score.php printed out. This is useful for troubleshooting and error handling.

Comments

0

try this

$(document).ready(function(){
        setInterval(function() {
        $.get("databaseUpdated.php");//or what ever your php file name is with corrct path
        return false;            
    }, 1000);
    });

hope this will help you use it in your function

function printResult() {
        var res;
        if(win[a.pos] === win[b.pos] && win[a.pos] === win[c.pos]) {
          // if    
            setInterval(function() {
            $.get("databaseUpdated.php");//or what ever your php file name is with corrct path
            return false;            
        }, 1000);

        } else {
            res = "You Lose";
        }
        $('#result').html(res);
    }

1 Comment

where should i add this exactly ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.