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

Solved FormAPI: get buttons' names

Discussion in 'Plugin Help' started by <Denny>, Jul 25, 2019.

  1. <Denny>

    <Denny> Spider

    Messages:
    7
    Hello, I don't know how to get clicked buttons name using FormAPI. I added with a foreach all the online players to the UI, but i don't know what names got selected and submitted. Can you help?
     
  2. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    send message to you for view what is returned in form why click in botton
    use:

    $player = your name;

    $player->sendMessage(print_r($data, 1));

    Send your code to i view and send code to you
     
  3. <Denny>

    <Denny> Spider

    Messages:
    7
    Sorry i can't understand what u said
     
  4. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    send me your code.

    The formapi returns values array $data use
    print_r($data, 1) to view value returned (name buttons )
     
  5. <Denny>

    <Denny> Spider

    Messages:
    7
    Code:
    $api = $this->getServer()->getPluginManager()->getPlugin("FormAPI");
        $form = $api->createCustomForm(function (Player $player, $data)  {
        if ($data === null) {
          return;
        }
        if ($data == true) {
          $player->sendMessage("Hi This is the button: " . print_r($data, true));
        }
      });
        $form->setTitle("test");
        $form->addLabel("select the players you want to send a message to");
        foreach ($this->getServer()->getOnlinePlayers() as $online) {
          if ($online !== $player) {
            $form->addToggle($online->getName());
          }
        }
     
  6. <Denny>

    <Denny> Spider

    Messages:
    7
    It says: array
    [
    0 =>
    1 => 1
    ]

    it shows up only the bool to tell me if the response is true or not but it doesnt show up any name
     
  7. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    Dropdown return value bool:
    0 or null for false (disabled)
    1 for true (enabled)

    Test my code:
    PHP:
    public $names = [];
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
       
    $form $api->createCustomForm(function (Player $player$data)  {
       if (
    $data === null) {
         return;
       }
    $num 0;
    foreach(
    $data as $n){
    if(
    $num != and isset($this->names[$player->getName()]) and $n == 1){
    $playerToMessage $this->names[$player->getName()][$num];
    /// code for player
    $num++;
    }
    }
    unset(
    $this->names[$player->getName()]);
       if (
    $data == true) {
         
    $player->sendMessage("Hi This is the button: " print_r($datatrue));
       }
      });
       
    $form->setTitle("test");
       
    $form->addLabel("select the players you want to send a message to");
    $num 1;
       foreach (
    $this->getServer()->getOnlinePlayers() as $online) {
         if (
    $online !== $player) {
           
    $form->addToggle($online->getName());
        
    $this->names[$player->getName()][$num] = $online;
    $num++;
         }
       }
    Good luck
     
    Last edited: Jul 26, 2019
  8. <Denny>

    <Denny> Spider

    Messages:
    7
    I put $playerToMessage->sendMessage("hi") but it doesn't send any message
     
  9. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    I have no way to test but I will make the code send some messages and you send me to identify the error

    PHP:
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
       
    $form $api->createCustomForm(function (Player $player$data)  {
       if (
    $data === null) {
         return;
       }
    $num 0;
    foreach(
    $data as $n){
    if(
    $num != and $n == 1){
    $playerToMessage $this->names[$player->getName()][$num];
    /// code for player
    $num++;
    }
    }
    $player->sendMessage(print_r($this->names[$player->getName()], 1));
    unset(
    $this->names[$player->getName()]);
       if (
    $data == true) {
         
    $player->sendMessage("Hi This is the button: " print_r($datatrue));
       }
      });
       
    $form->setTitle("test");
       
    $form->addLabel("select the players you want to send a message to");
    $num 1;
       foreach (
    $this->getServer()->getOnlinePlayers() as $online) {
         if (
    $online !== $player) {
           
    $form->addToggle($online->getName());
        
    $this->names[$player->getName()][$num] = $online;
    $num++;
         }
       }
     
  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.