1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to get the button clicked in FormAPI

Discussion in 'Development' started by Levi, Apr 27, 2019.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    How do I get what the button name was clicked using FormAPI?
    i create buttons with
    PHP:
    $form $this->getFormAPI()->createSimpleForm( function ( Player $player, ?int $result null ) {
                     
                        if ( 
    $result === null ) {
                            return;
                        }
                     
                        switch (
    $result) {
                            case 
    0:
                                
    $player->sendMessage("hi");
                                break;
                        }
                     
                        return;
                    } );
                    
    $form->setTitle$title );
                    
    $form->setContent$content );
                    
    $form->addButton"hello");
                    
    $form->addButton"hey");
                    
    $form->sendToPlayer$player );
    how do i get what button the player clicked
    like if(buttonclicked == "hello"){}
     
    Last edited: Apr 27, 2019
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    1. button that you'll add gets index 0
    2. button that you'll add gets index 1
    3 ... 2
    ...

    in function that you will add to the form you use $result instead of index

    so hello button has index 0
    PHP:
    if($result === 0) {

     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    I know but i want to know the name of the button they clicked
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Store the name of your buttons. You can get it from the closure using the use() construct.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.