Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!