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

266
Visualizações
Get value of an element inside a div printed by PHP

I have a code in PHP that prints HTML elements based on the information we got in the database, so if we have for example 8 products the code will print 8 divs with the following elements inside

  • h1 the id of this element will be the same as the product in the database
  • input for client name
  • buy button that send the ID of the H1 to the database

here is some code i made

   <?php
    
    while($row=$consulta->fetch())
    {
        ?> <img src="<?php echo $row["Imagen"];?>"style="width:12rem;height:12rem;" alt="Scotter" > 
        <br>
        <div>

        
        
        <h4 id="<?php echo $row["ID_Scooter"]; ?>"> <?php echo $row["ID_Scooter"]; ?> </h4> 
        <br>

        <?php
        echo $row["Nombre"];
        ?>

        <br>

        <?php
        echo $row["Descripcion"];
        ?>

        <br>
        
        <?php

        ?>
        <button onclick="abrir()">Alquilar</button>

        <br> 
    </div>
        <?php

    } 

?>

JavaScript

 function abrir(){
       var dialog = document.getElementById('favDialog');  
    dialog.show();
    
    var id=document.getElementById("heres is where im supposed to get the id of the id i clicked");

alert("div id is="+id);
    }

 

php assing ID for example (3,4,5,6,7) but when i click the button inside the div with id=5 it prints the id=3 no matter which button i click

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

please note that you've set onclick="abrir()" without passing any specific data!

you have got 2 way to do it:

1- passing data with php:

HTML:

abrir(<?php echo $row["ID_Scooter"]; ?>)

and use it in your js :

function abir(id){
let id_scooter = id;
...
}

2- passing clicked element with js:

HTML:

onclick="abrir(this)"

JS:

function abir(el){
  let id_scooter = (el).attr(id);
  ...
  }
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