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

Cant Get Data from FormAPI

Discussion in 'Development' started by Nemo404, Aug 23, 2018.

  1. Nemo404

    Nemo404 Spider Jockey

    Messages:
    39
    GitHub:
    chaostixzix
    Hi, i'm new at pmmp and i used this plugin called FormAPI, and im trying to get $data when making form.

    Here's the code

    PHP:
    public function ChooseClass(Player $player)
    {
        
    $rpgClass = [TextFormat::DARK_GRAY 'Rogue'TextFormat::AQUA 'Mage'TextFormat::YELLOW 'Warrior'];
        
    $form $this->form->createCustomForm(function (Player $sender, array $data) {
            
    $sender->sendMessage($rpgClass[$data[0]]);
        }); 
    // Sends all data to console
        
    $form->setTitle(TextFormat::GOLD "Select Your Class");
        
    $form->addDropdown("Class"$rpgClass);
        
    $form->sendToPlayer($player);
    }

    I cant send the message to the player because the $rpgClass variable is outside.
    so, How to get the $data outside the Function inside the $form

    Sorry for my bad english
     
  2. Qeis2007

    Qeis2007 Witch

    Messages:
    70
    GitHub:
    FrostingBita
    You will need to explain what is the API, so change with this:
    PHP:
    public function ChooseClass(Player $player)
    {
        
    $rpgClass = [TextFormat::DARK_GRAY 'Rogue'TextFormat::AQUA 'Mage'TextFormat::YELLOW 'Warrior'];
        
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
        
    $form $api->createCustomForm(function (Player $sender, array $data) {
            
    $sender->sendMessage($rpgClass[$data[0]]);
        }); 
    // Sends all data to console
        
    $form->setTitle(TextFormat::GOLD "Select Your Class");
        
    $form->addDropdown("Class"$rpgClass);
        
    $form->sendToPlayer($player);
    }
     
  3. Nemo404

    Nemo404 Spider Jockey

    Messages:
    39
    GitHub:
    chaostixzix
    i Already use the API on __construct
    PHP:
    public function __construct(Main $main)
    {
        
    $this->form $main->getForm();
    }
    and the form is working. the problem is, i cant get the $data after the player submit the form
    this is getForm() function
    PHP:
    public function getForm()
    {
        
    $this->form = new FormAPI($this);
        return 
    $this->form;
    }
    i didnt use FormAPI as new plugin, i just add it to my code
     
  4. Qeis2007

    Qeis2007 Witch

    Messages:
    70
    GitHub:
    FrostingBita
    :eek:, i forgot that code, i will try to remember it
     
  5. Nemo404

    Nemo404 Spider Jockey

    Messages:
    39
    GitHub:
    chaostixzix
    i can use
    PHP:
    public function ChooseClass(Player $player)
    {
        
    $form $this->form->createCustomForm(function (Player $sender, array $data) {
            
    var_dump($data);
        });
        
    $form->setTitle(TextFormat::GOLD "Select Your Class");
        
    $form->addDropdown("Class"$this->getClass());
        
    $form->sendToPlayer($player);
    }
    but, i want to use the $data outside the $form variable
     
  6. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    check if the $data variable is not null and send the message.
     
  7. Nemo404

    Nemo404 Spider Jockey

    Messages:
    39
    GitHub:
    chaostixzix
    Its not, i use var_dump here
    PHP:
    public function ChooseClass(Player $player)
    {
        
    $form $this->form->createCustomForm(function (Player $sender, array $data) {
            
    var_dump($data);
        });
        
    $form->setTitle(TextFormat::GOLD "Select Your Class");
        
    $form->addDropdown("Class"$this->getClass());
        
    $form->sendToPlayer($player);
    }
    i just wanna know if i could use like this

    PHP:
    public function ChooseClass(Player $player)
    {
        
    $form $this->form->createCustomForm(function (Player $sender, array $data) {
          
        });
        
    $form->setTitle(TextFormat::GOLD "Select Your Class");
        
    $form->addDropdown("Class"$this->getClass());
        
    $form->sendToPlayer($player);
        
    var_dump($data);
    }
     
  8. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    PHP:
        $form $this->form->createCustomForm(function (Player $sender, array $data) use ($rpgClass)  {
            
    $sender->sendMessage($rpgClass[$data[0]]);
        });
     
  9. Nemo404

    Nemo404 Spider Jockey

    Messages:
    39
    GitHub:
    chaostixzix
    Thanks man. But, can i use the $data outside the function?
     
  10. Nemo404

    Nemo404 Spider Jockey

    Messages:
    39
    GitHub:
    chaostixzix
    NVM, i found the way
     
  11. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    Why would you do that? Why not handle the form response inside the callable?
     
  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.