Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
select info from database to ul by id and open it in new window

i have this code to select some info from database to ul and open it in new window by id , the problem here is the same row open in the new window even i clicked in another title , i just want if user click in title will open new window with the info from the same row in database and if user click another title will open another window withe info from the same row :

<div class="col l6 offest-13">
 <?php 

   $sql = "SELECT * FROM post";
   $result = mysqli_query($conn, $sql);
   if(mysqli_num_rows($result) > 0){
   while($row = mysqli_fetch_assoc($result)){ 
       
?>
    <ul class="collection" >
        
        <li class="collection-item" >
            <h3><a id="a" value="submit" name="submit" href="" ><?php echo 
        $row['title'];?></a></h3>
            <span class="grey-text">by <?php echo $row['author'];?> on <?php echo 
        $row['publish_date'];?></span>
            <br>
        <button class="button" id="submit" name="submit" value="submit"><span 
        class="s_text">GET COINS</span></button>
        <div class="time-views">
            <span class="time"><?php echo $row['type'];?>s<i class="material- 
        icons">access_time</i></span>
            <span class="views"><?php echo $row['views'];?><i class="material- 
         icons">visibility</i></span>
            </div> 
        

     </li>
        
      </ul>
       <script type="text/javascript">
           $( "#a" ).click(function(){
                window.open("createdb.php?id=<?php echo $row['id'];?>","ASDF") ;
      });

      </script>
       <?php                 
        
    }
}

?>    
  
</div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Since you are creating multiple items and assigning id="a" for all of them, it will always open first element, even if you click on second or third etc. When you are looping through database, you can assign id of that row to link's id like this

`<h3><a id="<?php echo $row['id'];?>" value="submit" name="submit" href="" ><?php echo $row['title'];?></a></h3>`

And then add script

<script>
$( "#<?php echo $row['id']?>" ).click(function(){
    window.open("createdb.php?id=<?php echo $row['id'];?>","ASDF") ;
});
<script>

but better way is to put script out of the loop

 $('.collection .collection-item h1 a').click(function(){
  window.open("createdb.php?id=" + this.id ,"ASDF") ;
 });
about 4 years ago · Juan Pablo Isaza Relatório

0

You dont need to put your script within while loop. It seems that n number of events are created according to the length of the array.

Solution:-

  1. Put your script out of the loop

  2. Add data attribute in the anchor tag and put the id in it (<a data-id="<?php echo $row['id']?>").

  3. Get the clicked elements id ( $(this).data("id") ) and pass createdb.php?id=$(this).data("id")

  4. Use class for click event instead of id because id is unique for every event

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda