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

286
Views
How to hide and show data fetched from database and displayed on the page, and show each row from table on button click

I' am fetching data from database with a PHP code. And what I want is to hide that data and show it on click each row on table on its own, like when click a particular row it shows and the other hide. I have a JavaScript code to hide and show that data but the problem is on click the only the first row of table that is working and all the button affect only the first row fetched. What I want is every row shows on click and the other hides.

The two row needed from table are named "name" and "info".

PHP code

<?php      
        $result = mysqli_query($db,"select * from table");
        
        while($data = mysqli_fetch_array($result)) { 
            
            echo "<button onclick='myFunction()'>";
            echo "<h3>" . $data["name"] ; "</h3>";
            echo "</button>";

            echo "<div id='hide'>";
            echo "<p>" . $data["info"] ; "</p>"; 
            echo "</div>";
            
        }
    ?>

And JavaScript code:

<script>
   function myFunction() {
   var x = document.getElementById("id");
      if (x.style.display === "none") {
          x.style.display = "block";
      } else {
            x.style.display = "none";
      }
   }
</script>

And even when I change the div to class and the first JavaScript code to another one below, the problem still with another behavior:

<script>
   function myFunction() {
    var myClasses = document.querySelectorAll('.hide'),
    i = 0,
    l = myClasses.length;

    for (i; i < l; i++) {
        myClasses[i].style.display = 'block';
    }

  }
</script>

That second JavaScript code hide and show the whole the data coming.

about 4 years ago · Juan Pablo Isaza
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!