Pick element selector created at runtime

Asked

Viewed 151 times

0

I have a system that lists the fields and attributes of a table through javascript and ajax up to there everything, only that the elements created and listed in precise runtime manipulates lós through a selector as I do to get this selector as 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>      
    ';
   }
}

through it I select the bank and the table and it lists for me its fields and attributes I need to get their id when they are created to trigger an event with the button and be able to change the table, I appreciate the help.

inserir a descrição da imagem aqui

this ID there that I want to take when I choose the bank and the table it is generated.

1 answer

0

I don’t know if I understand very well, but you can leave the buttons with the same name and add the id you want to pick up, in the case there you were leaving the id’s equal which is not correct, because id has to be unique, name may repeat, in all your inputs is also repeating the id this is wrong, it works, but it’s 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);
    });
}); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.