Updating User Currency without a Refresh

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
RogueTomboy
Posts: 61
Joined: Sun May 22, 2011 3:42 pm

Updating User Currency without a Refresh

Post by RogueTomboy »

I was wondering what the best way would be to go about making the users gold/etc be able to update without a refresh?

Example:
User just deposited X amount of gold/etc into their bank and hit submit. The bank codes all update and let the user know that their deposit went through and they now have X amount of gold/etc in their bank.

The page was not refreshed.

The user can always see their gold/etc amount in the constant stat panel I have in my layout - so I was wondering how I could get that to show the loss of gold/etc when they visit the bank or even a store (once that is built) without having to refresh the page.

Their currency right now is simply being shown by

Code: Select all

<?php 
echo $characterinfo3['credits'];
?>
Need art? Commissions are always open!
http://www.ladytankstudios.com

Currently addicted to:
Image
Collect & Explore for pets
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Updating User Currency without a Refresh

Post by Jackolantern »

This is a classical case to use AJAX techniques of Javascript. Anything to be done without a refresh must be client-side coding. While there is Flash, Silverlight and others, by far the easiest and friendliest to use for PHP coders is AJAX. Your best bet is to cozy up with jQuery, the leading Javascript library. There is too much to go over to explain fully how to use it here, but you could get a handle on it from the tutorials on the jQuery site, and then we can help you if you have any issues after that :)
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Updating User Currency without a Refresh

Post by hallsofvallhalla »

Code: Select all

<div id ='money'>

</div>

 <script language="javascript">
var money = "<?php echo $playerinfo3['money']; ?>";

function init()
{
updatemoney(0);
}

function updatemoney(amount)
{
money = money + amount;
document.getElementById('money').innerhtml = "Money: " + money;
}

</script>


<body onload="init()">
and every time you want to update money

Code: Select all

 <script language="javascript">
updatemoney(10);
</script>
where 10 will equal how much you want to add or subtract.

Not sure if negitives will take but all you got to do is make a second pass in variable that will distingush it.

if you are using jquery then instead of documentbyid you use

Code: Select all

$("#money").html(money); 
yes I have been using jquery...tricksy little hobbits.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Updating User Currency without a Refresh

Post by Jackolantern »

Ohhh oops. I was thinking of a server hit without a refresh, aka AJAX :o Yeah, altering the page without a refresh is not so hard.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Updating User Currency without a Refresh

Post by hallsofvallhalla »

well you will most likely need to add ajax. But that will result in returning the money from the query into ajax.response then running the function
aka
updatemoney(ajax.response);

in which you will need to remove the money + amount and instead make it
money = amount;
User avatar
RogueTomboy
Posts: 61
Joined: Sun May 22, 2011 3:42 pm

Re: Updating User Currency without a Refresh

Post by RogueTomboy »

Thanks guys, I'll tinker with it and see what happens :)
Need art? Commissions are always open!
http://www.ladytankstudios.com

Currently addicted to:
Image
Collect & Explore for pets
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Updating User Currency without a Refresh

Post by Jackolantern »

hallsofvallhalla wrote:yes I have been using jquery...tricksy little hobbits.
Finally! ;)
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Updating User Currency without a Refresh

Post by Xaleph »

Hehe nice :)

Anyway, one thing: var money = "<?php echo '';?>";

Drop the quotes. Javascript handles numbers as integers. Saves time and memory ( be it 0.0001microseconds ) anyway, it`s good practice to uses the right datatypes even though it`s not strictly seperated in js.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Updating User Currency without a Refresh

Post by hallsofvallhalla »

I don't think it will work without quotes. I have had issues with that in the past.
Post Reply

Return to “Beginner Help and Support”