Jump to content

Just a quick question (run a script when button clicked)


Bikkebakke

Recommended Posts

So, I currently have a while loop to run script till certain condition is met but I'd want to run the script by clicking on a button to be able to add a "cancel" button.

To be more exact, I'm coding a simple game and the battles run a while loop where both parties hit eachother untill other ones HP goes <=0 and the option I want to add would be a run/flee button so the player could run away if it seems the monster is too strong.

 

I know it's simple but as I've never done this before I don't know how to implement the button. HTML I guess but I don't know how to insert the script to run on click.

 

I'd appreciate it if someone pointed me out an easy way to do this. :)

Is that necessary?

 

The code goes like:

while ( $playerhp >= 0 && $monsterhp >= 0 ) {
echo "$playername attacks $monstername for ".$playerdamage." damage! <br>";
echo "$monstername attacks $playername for ".$monsterdamage." damage! <br>";
$monsterhp -= $playerdamage;
$playerhp -= $monsterdamage;
mysql_query("UPDATE users SET hp = {$playerhp}  WHERE id = {$_SESSION['user_id']}");
}

 

So instead I'd want a button:

[how-to]BUTTON ("Attack!", onclick run:)[/how-to]

if ( $playerhp >= 0 && $monsterhp >= 0 ) {
echo "$playername attacks $monstername for ".$playerdamage." damage! <br>";
echo "$monstername attacks $playername for ".$monsterdamage." damage! <br>";
$monsterhp -= $playerdamage;
$playerhp -= $monsterdamage;
mysql_query("UPDATE users SET hp = {$playerhp}  WHERE id = {$_SESSION['user_id']}");
} else {
-check which one has >= 0 hp and echo a "You died" or "monster died" message-
}

 

 

Is that possible with HTML/PHP, or do I need to use something like javascript or ajax?

 

EDIT: I took a look at Ajax and it seems that's the best (and prolly only) way to do this, I'll read a tutorial or two, Ajax surely seems like something I could make a BIG use of. Thanks for pointing that out, thorpe :)

 

Also, if you have any advice, I'll be glad to hear since I don't know Ajax at all. :o

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.