Page 1 of 1
Updating User Currency without a Refresh
Posted: Tue Jul 26, 2011 1:46 pm
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'];
?>
Re: Updating User Currency without a Refresh
Posted: Tue Jul 26, 2011 6:15 pm
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

Re: Updating User Currency without a Refresh
Posted: Tue Jul 26, 2011 7:51 pm
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
yes I have been using jquery...tricksy little hobbits.
Re: Updating User Currency without a Refresh
Posted: Tue Jul 26, 2011 8:03 pm
by Jackolantern
Ohhh oops. I was thinking of a server hit without a refresh, aka AJAX

Yeah, altering the page without a refresh is not so hard.
Re: Updating User Currency without a Refresh
Posted: Tue Jul 26, 2011 8:15 pm
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;
Re: Updating User Currency without a Refresh
Posted: Tue Jul 26, 2011 9:16 pm
by RogueTomboy
Thanks guys, I'll tinker with it and see what happens

Re: Updating User Currency without a Refresh
Posted: Wed Jul 27, 2011 12:10 am
by Jackolantern
hallsofvallhalla wrote:yes I have been using jquery...tricksy little hobbits.
Finally!

Re: Updating User Currency without a Refresh
Posted: Thu Jul 28, 2011 3:09 pm
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.
Re: Updating User Currency without a Refresh
Posted: Thu Jul 28, 2011 3:19 pm
by hallsofvallhalla
I don't think it will work without quotes. I have had issues with that in the past.