Page 1 of 1

How do we keep refreshing a certain part of the page with JQ

Posted: Wed Feb 05, 2014 12:53 pm
by Gunner
So I have an excluded file called PlayerInfo.php
This file later on gets included in every page in my game. It shows how much does the player have HP or MP or Gold left.

Everytime the player buys something from the store, sometimes the 'gold bar' still shows the old gold.. I mean if you had 30 gold and you spent 15, it still shows 30 unless it gets refreshed first..

Same thing happens with the HP and MP bar when on the battle system..
So I want to refresh this part of div dynamically per x ms with jQuery.

I'm thinking of defining a function on JS first. and then put SetInterval in the end.

Can anyone show me the right way without screwing down the page?

Re: How do we keep refreshing a certain part of the page wit

Posted: Wed Feb 05, 2014 1:12 pm
by a_bertrand
You can certainly use ajax to load back data from the server and update divs or others dynamically. However keep in mind, that this will use some resources. So avoid to do too many Ajax call ;)

Re: How do we keep refreshing a certain part of the page wit

Posted: Wed Feb 05, 2014 1:31 pm
by Gunner
ok, but why not? I thought those are one effective ways?

Re: How do we keep refreshing a certain part of the page wit

Posted: Wed Feb 05, 2014 1:55 pm
by a_bertrand
If your update is based on calculation, you could simply send while the page load the number of sec it needs to wait till completion and only then reload to show the new effect. This is just an example BTW.

Re: How do we keep refreshing a certain part of the page wit

Posted: Thu Feb 06, 2014 6:42 am
by Winawer
Why not update the gold when the player buys stuff? Your example doesn't really require polling for anything.

Re: How do we keep refreshing a certain part of the page wit

Posted: Thu Feb 06, 2014 7:11 am
by Gunner
Winawer wrote:Why not update the gold when the player buys stuff? Your example doesn't really require polling for anything.
I already did this. I even put the update query on top of everything, but for some reason in PHP it just can't :? (although in some part I found it working without I even know why lol)

So a_bertrand, since I have just gotten into jQuery and JScript, I might have to greet the codes more to get familiar with. So I have some question also about jQuery in general. Frankly, AJAX. Also could you suggest me which one should I use best? The jQuery post or simply the AJAX itself?

If I have this

Code: Select all

$.ajax({

});
what commands do I need to get the data from database? I mean like url, method, datatype, etcetera. But to be quite honest I can't really see it working. How does it send data to the url.php?

And, for example in url.php I echo some variable. Will it be able to be shown in the initial page? Mainly it's because I just moved from PHP and I think the core mechanic is far different in signing form.

Oh FYI, I think I already found one good code to do it, but they just 'do' what I want, without I understand them. And most sources don't provide enough info about what does each line of code do. So it would be good if I get to know how it works ;)

THanks!

Re: How do we keep refreshing a certain part of the page wit

Posted: Thu Feb 06, 2014 7:25 am
by a_bertrand
Check out how to implement ajax in PHP, there is TONS of examples and tutorials. It's not overall complex, mostly it's like loading a normal page (for the server it is actually exactly that), however instead of displaying that page, you use the result differently.

Re: How do we keep refreshing a certain part of the page wit

Posted: Thu Feb 06, 2014 8:21 am
by Winawer
Gunner wrote:
Winawer wrote:Why not update the gold when the player buys stuff? Your example doesn't really require polling for anything.
I already did this. I even put the update query on top of everything, but for some reason in PHP it just can't :? (although in some part I found it working without I even know why lol)
Of course it can, submit form->update data->show data is pretty much the most common thing that's done in PHP. Your code is just wrong.

Re: How do we keep refreshing a certain part of the page wit

Posted: Thu Feb 06, 2014 10:47 pm
by Jackolantern
Gunner wrote:I already did this. I even put the update query on top of everything, but for some reason in PHP it just can't :? (although in some part I found it working without I even know why lol)
As has already been mentioned, PHP will work just fine for this. You don't need to alter the display of the gold unless the player does something. If the page reloads, you can just change it in the PHP and then redisplay the correct amount on the page load. If you do it through AJAX, you would just return the new gold amount directly to the Javascript on the client-side and use Javascript to update the amount in the browser.
Gunner wrote:Also could you suggest me which one should I use best? The jQuery post or simply the AJAX itself?
There are few differences. $.post() is just a shortcut function that sets up some default values to the $.ajax() function.

In most cases you are fine to use $.post() or $.get(), but $.ajax() does have some extra options that aren't usable in the shortcut functions, such as events for each stage and result of the AJAX call. For this reason you will find some developers always use $.ajax, even when they don't need the extra functionality. But it is up to you. I extensively use the shortcut functions myself since they are just less typing and a bit more concise.

Re: How do we keep refreshing a certain part of the page wit

Posted: Sat Feb 08, 2014 7:10 pm
by Gunner
You were right, PHP could make this really easily. I just didn't notice that I put the query in the middle of my body tag, whereas the sidebar containing player hp mp gold etc etc were in the left sidebar so they get executed first! Damn it worked like an epic charm *sigh*

Yay so the idea to do that with JQ is out. But I still need to stick with it as it seems really handy and I will just use the $.ajax

thanks for your time everyone ;) appreciated