0

I’m creating a Javascript game and I’m currently trying to write some code that will show the player’s “Gold Balance” in real time on a html webpage.

The Gold amount is contained in my SQL database, so I’m using setInterval with a Javascript function that contains an AJAX call which calls a PHP script that grabs the current balance amount for the player and sends it back as “response”.

I’m able to have this amount appear as a Javascript alert, however I need to have the response appear as text on the webpage inside a <div> instead.

This is my current code:

<script>
setInterval("checkGold()",5000); 

function checkGold()
{
        $.ajax({
     url: 'scripts/checkGold.php',
    data: "",
    dataType: 'json',
    success: function(response){
                        alert(response);       
    }});
};
</script>

I have this in my html source code, I would like to place the function in a separate file and call it from there, but when I tried this I wasn't able to send the response back to the html page correctly.

I was wondering if anyone knows how to have this response appear as text on the page inside <div> </div>?

Also, I was wondering if this method will really update the div in real time (ie, will it auto-refresh the div part of the webpage, showing an up to date value (every 5000 milliseconds)?

Thanks in advance!

1

2 Answers 2

0

Since you are using jQuery, you can use text() to alter the contents of an existing div (which id is "yourDiv"):

setInterval("checkGold()",5000); 

function checkGold()
{
    $.ajax({
      url: 'scripts/checkGold.php',
      data: "",
      dataType: 'json',
      success: function(response){
        $('div#yourDiv').text(response);       
      }
    });
};
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @iran-rodrigues thanks for your reply, I'm trying your method but I think I'm making a mistake, I'm using your code, and have set the div to: <div id="yourDiv"></div> however it's not working for me, I think I'm doing something very basic wrong?
0

You have two questions here, so I will try to address both

1) How to append to the DOM using jQuery, instead of an alert:

  • in the success callback function, instead of alerting the response, you can simply call

    $('body').append("<div>"+response+"</div>")
    

2) "Real time" Gold Balance

  • You should use websockets. Racthet is a good websocket PHP library to help you with this: http://socketo.me/

1 Comment

Hi @user3191753 thanks for your reply. I'm trying your code in the success callback function but I don't think I'm doing it right. Do I need to write something in the <div> </div> tags in the html to make this method work? Also, the response from the database will be different for every player (the PHP passed contains a unique player ID), so would websockets still work for this? Or is the AJAX way a better method? I might have misunderstood the websockets link but is that only for updating multiple users with the same data value? (for example, a blog post)

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.