Author Topic: Just a quick question (run a script when button clicked)  (Read 262 times)

0 Members and 1 Guest are viewing this topic.

Offline BikkebakkeTopic starter

  • Irregular
  • Posts: 40
    • View Profile
Just a quick question (run a script when button clicked)
« on: February 10, 2010, 05:59:19 AM »
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. :)

Offline BikkebakkeTopic starter

  • Irregular
  • Posts: 40
    • View Profile
Re: Just a quick question (run a script when button clicked)
« Reply #1 on: February 10, 2010, 06:27:37 AM »
Or, do I need a javascript function to do that?  :shrug:

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,256
    • View Profile
Re: Just a quick question (run a script when button clicked)
« Reply #2 on: February 10, 2010, 06:29:35 AM »
You migth want to look into Ajax. PHP executes on the server, so your button will need to make another request back to the server in order for any php to be executed.

Offline BikkebakkeTopic starter

  • Irregular
  • Posts: 40
    • View Profile
Re: Just a quick question (run a script when button clicked)
« Reply #3 on: February 10, 2010, 06:39:58 AM »
Is that necessary?

The code goes like:
while ( $playerhp >= && $monsterhp >= ) {
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 >= && $monsterhp >= ) {
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 "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
« Last Edit: February 10, 2010, 06:45:22 AM by Bikkebakke »

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,256
    • View Profile
Re: Just a quick question (run a script when button clicked)
« Reply #4 on: February 10, 2010, 06:56:46 AM »
Quote
Also, if you have any advice, I'll be glad to hear since I don't know Ajax at all.

If you want my advice, use a framework like jQuery for your Javascript needs, it'll make things allot easier.

Offline BikkebakkeTopic starter

  • Irregular
  • Posts: 40
    • View Profile
Re: Just a quick question (run a script when button clicked)
« Reply #5 on: February 10, 2010, 06:59:40 AM »
Okay, I'll take a look at it.

Thanks for the answers, I'll mark the thread solved and start working on this. :)