PhpNobody Posted January 23, 2011 Share Posted January 23, 2011 Hello. I decided to create my own browser game and started learning PHP and HTML. Everything is going quite well now I'm creating a primitive version of the game. I'm reading tutorials and moving forward fast. But I started thinking about the combat system and got stuck. I want to let my users to choose which monster to attack with buttons. I made this form as an example: <p>Choose which monster you want to hunt:</p> Rat<br /> <form action='main.php?p=hunt' method='post'> <input type='submit' name='rat' value='Attack' /><br /><br /> Slime<br /> <form action='main.php?p=hunt' method='post'> <input type='submit' name='slime' value='Attack' /> </form> And I'd like to know what code should I use to know which submit button was pressed, the rat or slime one. I currently only know how to do a lot of IF's to determine it so is there a short way to do it? If anyone can, please help me, and if you can try to explain it simply as I'm still starting to understand php . And also I'd like some suggestions where to run my combat script (the same page where the form is or it should be on a different page, if the page should use id from a rand() function and so on). Thanks Quote Link to comment https://forums.phpfreaks.com/topic/225440-how-to-know-which-button-was-pressed/ Share on other sites More sharing options...
Simon Mayer Posted January 23, 2011 Share Posted January 23, 2011 You can do the following: if($_POST['rat'] == 'Attack') { //rat was attacked } elseif($_POST['slime'] == 'Attack') { //slime was attacked } which would get quite long if you have lots of attacks. Or you could do the following: HTML <p>Choose which monster you want to hunt:</p> <form action='main.php?p=hunt' method='post'> <input type='submit' name='attack' value='Rat' /><br /><br /> <input type='submit' name='attack' value='Slime' /><br /><br /> <input type='submit' name='attack' value='Something Else' /> </form> PHP switch($_POST['attack']) { case 'Rat': //rat was attacked break; //this is needed to prevent your slime attack scenario being called at the same time case 'Slime': //slime was attacked break; //this is needed to prevent your something else attack scenario being called at the same time case 'Something Else': //something else was attacked break;//break is optional for the final event, but it does no harm to leave it there } Quote Link to comment https://forums.phpfreaks.com/topic/225440-how-to-know-which-button-was-pressed/#findComment-1164147 Share on other sites More sharing options...
requinix Posted January 23, 2011 Share Posted January 23, 2011 You can also use arrays if (is_array($_POST["attack"])) { $what = key($_POST["attack"]); // rat or a , which lets you specify a value and caption separately Attack the rat if (isset($_POST["attack"])) { $what = (string)$_POST["attack"]; // rat Quote Link to comment https://forums.phpfreaks.com/topic/225440-how-to-know-which-button-was-pressed/#findComment-1164154 Share on other sites More sharing options...
PhpNobody Posted January 24, 2011 Author Share Posted January 24, 2011 Simon, I actually wanted to leave the same value everywhere and already knew of the way you offered though thanks. Requinix, your code does the trick for me so thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/225440-how-to-know-which-button-was-pressed/#findComment-1164568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.