How to know which button was pressed?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhpNub
Forum Newbie
Posts: 2
Joined: Sun Jan 23, 2011 3:53 pm

How to know which button was pressed?

Post by PhpNub »

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:

Code: Select all

<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 :oops: . 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

:)
Peec
Forum Commoner
Posts: 33
Joined: Fri Feb 22, 2008 3:58 am

Re: How to know which button was pressed?

Post by Peec »

As I can see in your HTML..

You would want to do:

In the file main.php

Code: Select all

<?php
if (isset($_GET['p']) && $_GET['p'] == 'hunt'){
    if (isset($_POST['rat'])){
        switch($_POST['rat']){
            case 'Attack':
                // Do stuff.
            break;
        }
    }
}
?>
PhpNub
Forum Newbie
Posts: 2
Joined: Sun Jan 23, 2011 3:53 pm

Re: How to know which button was pressed?

Post by PhpNub »

Thanks but I already have the code, using arrays ^^.
Post Reply