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

Get $data of DropDown

Discussion in 'Development' started by armagadon159753, Jan 2, 2018.

  1. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    my code works but when I click on the button "submit" nothing happens

    FormAPI
    PHP:
    public function playerSelector(Player $sender){
            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $api->createCustomForm(function (Player $sender, array $data){
            switch(
    $data[0]){
                case 
    0:
                
    $sender->sendMessage("test");
                break;
                case 
    1:
                
    $sender->sendMessage("test");
                break;
                case 
    2:
                
    $sender->sendMessage("test");
                break;
            }
            });
            
    $form->setTitle("");
            foreach(
    $this->getServer()->getOnlinePlayers() as $online){
                
    $name $online->getName();
            }
            
    $form->addDropdown("Player Liste:", array("1""2""3"));
            
    $form->sendToPlayer($sender);
        }
    other API

    PHP:
    public function playerSelector(Player $player){
            
    $ui = new CustomForm("Player Selector",".......");
            foreach(
    $this->getServer()->getOnlinePlayers() as $online){
                
    $name $online->getName();
            }
            
    $Dropdown = new Dropdown("Player Liste:", array($name));
            
    $ui->addElement($Dropdown);
            
    $pk = new ModalFormRequestPacket();
            
    $pk->formId 111;
            
    $pk->formData json_encode($ui);
            
    $player->dataPacket($pk);
            return 
    true;
        }
    PHP:
    public function DataPacketReceiveEvent(DataPacketReceiveEvent $event){
            
    $packet $event->getPacket();
            
    $player $event->getPlayer();
            if(
    $packet instanceof ModalFormResponsePacket){
                
    $id $packet->formId;
                
    $data $packet->formData;
                
    $data json_decode($data);
                if(
    $data === Null) return true;
                    if(
    $id === 111){
                        if(
    $data === 0){
                            
    $player->sendMessage("TEST");
                            return 
    true;
                        }
                    }
                }
            }
        }
     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    PHP:
    if($data === Null) return true;
                    if(
    $id === 111){
                        if(
    $data === 0){
                            
    $player->sendMessage("TEST");
                            return 
    true;
                    }
           }
    }
    calculate brackets
     
  3. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    that is to say ? I do not have syntax errors
     
  4. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    do you implement Listener?
     
  5. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    Yes
     
  6. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Try var_dump the result and you will see then.
     
  7. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    I already do that and the console display nothing
     
  8. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    You must var_dump it before you do the if() check
     
  9. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    PHP:
    int(3)
    array(
    1) {
      [
    0]=>
      
    int(0)
    }
     
  10. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    PHP:
    if(!is_null($data[0])){
    $player->sendMessage("Test");
    }
     
  11. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    After check if id === 111?
     
    Kyd likes this.
  12. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    Dont work after check if id === 111
     
  13. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
  14. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Your code looks fine to me, I modified it a bit and this worked for me:
    PHP:
    <?php
    //form api
    public function sendForm(Player $player) {
        
    $api $this->plugin->getServer()->getPluginManager()->getPlugin("FormAPI");
        if(
    is_null($api)) {
            return 
    False;
        }

        
    $function = function(Player $player, array $data) {
            if(empty(
    $data)) { //player closed the UI
                
    return;
            }

            switch(
    $data[0]) {
                case 
    0// this is "1"
                //code here;
                
    break;
                case 
    1// this is "2"
                //code here;
                
    break;
                case 
    2// this is "3"
                //code here;
                
    break;
            }
        };

        
    $form $api->createCustomForm($function);
        
    $form->addDropdown("Player Liste:", ["1""2""3"]);
        
    $form->sendToPlayer($player);
    }
     
    armagadon159753 likes this.
  15. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    is there any other way with the other API?
     
  16. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Never used the "other api", FormAPI will be discontinued soon I suggest using the 'forms-api' branch of PMMP.
     
    armagadon159753 likes this.
  17. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    I prefer to wait until the pull request is merge in the master branch

    Thx to you
     
    QuiverlyRivalry, SOFe and 0x15f like this.
  18. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    No problem.
     
  19. armagadon159753

    armagadon159753 Zombie

    Messages:
    217
    GitHub:
    armagadon159753
    I tested but it does not work
     
  20. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    The same way you provided the array for the dropdown can be used to get its value.

    So an example code would be:

    Code:
    $choosen = $previousArray[$data];
     
  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.