Catch element selector created at runtime

0

I have a system that clears the fields and attributes of a table via javascript and ajax so far, but the elements created and listed at the precise execution time manipulate them through a selector like I do to pick up that selector like id?

This is the code I use to display the fields:

    if($_POST['op']=='bancopesquisartabelas'){  
    $pdo = new PDO(DB_SERVER.":host=".DB_HOST.";dbname=".$_POST['bancotabelas'],DB_USER,DB_PASSWORD);
    $sql = $pdo->prepare("SHOW TABLES FROM ".$_POST['bancotabelas']);
    $sql->execute();    
    foreach($sql as $obj){
        echo '<option value="'.$obj[0].'">'.$obj[0].'</option>';
 }  $pdo = null;

 } else if ($_POST['op']=='mostarcampos'){

 $_SESSION['bancotabelas']=$_POST['bancotabelas'];
 $_SESSION['tabelasbanco']=$_POST['tabelasbanco']; 

       $pdo = new PDO(DB_SERVER.":host=".DB_HOST.";dbname=".$_POST['bancotabelas'],DB_USER,DB_PASSWORD);
       $sql = $pdo->prepare("SHOW FIELDS FROM ".$_POST['tabelasbanco']); 
       $sql->execute();
       foreach($sql as $obj){  
    echo'
        <input id="'.$obj[0].'" type="text" class="form-control" value="'.$obj[0].'" placeholder="'.$obj[0].'" style="width:150px">
        <input id="'.$obj[1].'" type="text" class="form-control" value="'.$obj[1].'" placeholder="'.$obj[1].'" style="width:150px">
        <input id="'.$obj[2].'" type="text" class="form-control" value="'.$obj[2].'" placeholder="'.$obj[2].'" style="width:150px">
        <input id="'.$obj[3].'" type="text" class="form-control" value="'.$obj[3].'" placeholder="'.$obj[3].'" style="width:150px">
        <input id="'.$obj[4].'" type="text" class="form-control" value="'.$obj[4].'" placeholder="'.$obj[4].'" style="width:150px">
        <input id="'.$obj[5].'" type="text" class="form-control" value="'.$obj[5].'" placeholder="'.$obj[5].'" style="width:150px">
        <button id="updatetabela" class="btn btn-default">Alterar</button>      
    ';
   }
}

>

This ID is what I want to get when I choose the database and the table it generates.

    
asked by anonymous 25.02.2017 / 20:38

1 answer

0

I do not know if I understand very well, but you can leave the buttons with the same name and add the id you want to get, in case you were leaving the id's the same as the one that is not correct, because id has to be unique , name can repeat, in all its inputs is also repeating the id this is wrong, it works, but it is wrong this, Ex:

<button name="updatetabela" id="'.$obj[0].'" class="btn btn-default">Alterar</button>

Assuming you are using Jquery

$(document).ready(function(){
    $('button[name=updatetabela]').click(function(){
         console.log(this.id);
    });
}); 
    
26.02.2017 / 19:38