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

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post 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?
Skype: mrvictordiaz
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

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

Post 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 ;)
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post by Gunner »

ok, but why not? I thought those are one effective ways?
Skype: mrvictordiaz
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

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

Post 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.
Creator of Dot World Maker
Mad programmer and annoying composer
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

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

Post by Winawer »

Why not update the gold when the player buys stuff? Your example doesn't really require polling for anything.
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post 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!
Skype: mrvictordiaz
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

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

Post 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.
Creator of Dot World Maker
Mad programmer and annoying composer
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

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

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

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

Post 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.
The indelible lord of tl;dr
User avatar
Gunner
Posts: 276
Joined: Sun Aug 14, 2011 12:07 am

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

Post 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
Skype: mrvictordiaz
Post Reply

Return to “Beginner Help and Support”