Bikkebakke Posted February 10, 2010 Share Posted February 10, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191600-just-a-quick-question-run-a-script-when-button-clicked/ Share on other sites More sharing options...
Bikkebakke Posted February 10, 2010 Author Share Posted February 10, 2010 Or, do I need a javascript function to do that? Link to comment https://forums.phpfreaks.com/topic/191600-just-a-quick-question-run-a-script-when-button-clicked/#findComment-1009985 Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191600-just-a-quick-question-run-a-script-when-button-clicked/#findComment-1009986 Share on other sites More sharing options...
Bikkebakke Posted February 10, 2010 Author Share Posted February 10, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191600-just-a-quick-question-run-a-script-when-button-clicked/#findComment-1009992 Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191600-just-a-quick-question-run-a-script-when-button-clicked/#findComment-1009997 Share on other sites More sharing options...
Bikkebakke Posted February 10, 2010 Author Share Posted February 10, 2010 Okay, I'll take a look at it. Thanks for the answers, I'll mark the thread solved and start working on this. Link to comment https://forums.phpfreaks.com/topic/191600-just-a-quick-question-run-a-script-when-button-clicked/#findComment-1010000 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.