Help - Search - Members - Calendar
Full Version: Updating An Object's Value On Click
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
The Seventh Hokage
i have a function that works... well kind of.

i have it set that it runs on click. it utilizes some ajax and javascript to print php to the page without refreshing.

the problem is when i click the button the same output shows up each time.

what i want is the number to decrease and the new value to be available

this is the actual code

CODE

function smack($target)
{
$dmg=$this->ATT-=$target->DEF;//amount of HP loss
$newHP=$target->HP-=$dmg;// HP after dmg
echo $target->HP." hit points remaining.";
$target->HP=$newHp;

}


this is what it outputs


This is only a test.100 .87 hit points remaining.

This is only a test.100 .87 hit points remaining.

This is only a test.100 .87 hit points remaining.

This is only a test.100 .87 hit points remaining.

This is only a test.100 .87 hit points remaining.

This is only a test.100 .87 hit points remaining.

This is only a test.100 .87 hit points remaining.


this is what i would like it to say



This is only a test.100 .87 hit points remaining.

This is only a test.100 .74 hit points remaining.

This is only a test.100 .61 hit points remaining.

... and so on

note that this is outputted to the page on button press. with ajax to write to the page without refreshing. I'm not sure if that's the cause of this or not. any help would be appreciated.
swordz
How are you storing the hit points value from AJAX request to AJAX request?

Swordz
The Seventh Hokage
QUOTE(swordz @ Aug 26 2009, 11:03 AM) *
How are you storing the hit points value from AJAX request to AJAX request?

Swordz

my assumption was that once the value got changed in the object i wouldnt need to carry it from ajax request to ajax request.

for instance if i have a object variable equal to 100.

and i used an ajax request to run a php script to change that object variable by -1 upon being called...
i just assumed that the object would have a new value of 99.

is that a wrong assumption?
swordz
Any internal script variables (including objects) are destroyed when the script ends. So yes, that is a wrong assumption. Unless you're storing the object in the $_SESSION variable.

Swordz
The Seventh Hokage
QUOTE(swordz @ Aug 26 2009, 05:18 PM) *
Any internal script variables (including objects) are destroyed when the script ends. So yes, that is a wrong assumption. Unless you're storing the object in the $_SESSION variable.

Swordz

ok thanks.

so i need to come up with a work around using a session var. thanks
The Seventh Hokage
ok so apparently it's more difficult than i thought it would be. could i get an example of how i might go about doing that?

this is what i have but it doesn't seem to be working.

i added session start to all the pages.

and then..

CODE
$_SESSION['HP']=100;//declare session var

function smack($target)
{
$dmg=$this->ATT-=$target->DEF;
$_SESSION['HP']-=$dmg;
echo $_SESSION['HP']." hit points remaining.";
}


but it still does the same thing.
swordz
Every time you call this script you're resetting the health.

Try:
CODE
if(!isset($_SESSION['HP'])) $_SESSION['HP'] = 100;


Swordz
The Seventh Hokage
ok thanks. i just tried that out. it does work. im not sure i totally understand how to use isset so im going to go study it. thanks for the help it's much appreciated
Ed
Probably worth pointing out that browser requests tend to get cached if the url is the same; if it is, it serves the page from the cache.

Quick fix is to append a timestamp to the url:

CODE

   var reqObj = false;
   try{reqObj = new ActiveXObject("Microsoft.XMLHTTP");}
   catch(e){reqObj = new XMLHttpRequest()}

   if(reqObj)
   {
      reqObj.open(this.methodType, 'somescript.php?' + (new Date()).getTime(), true);
   }

   // etc...


Alternatively you can tell your script to not be cached:

CODE
header('Expires: Sat, 1 Jan 2000 00:0:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: No-cache');
The Seventh Hokage
QUOTE(Ed @ Aug 27 2009, 07:15 PM) *
Probably worth pointing out that browser requests tend to get cached if the url is the same; if it is, it serves the page from the cache.

Quick fix is to append a timestamp to the url:

CODE

   var reqObj = false;
   try{reqObj = new ActiveXObject("Microsoft.XMLHTTP");}
   catch(e){reqObj = new XMLHttpRequest()}

   if(reqObj)
   {
      reqObj.open(this.methodType, 'somescript.php?' + (new Date()).getTime(), true);
   }

   // etc...


Alternatively you can tell your script to not be cached:

CODE
header('Pragma: No-cache');



ok thanks i'll try that too
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.