Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Stuck on PHP JavaScript interaction

$
0
0

ive been working on this code for a while now and have been stuck several times but this time around I think Im stuck for real.

I'm not a coder, I just enjoy it as a hobby so dont get to frustrated over how my code is looking, Im trying to improve.

What Im trying to do is to create multiple boxes that shows information from a database, I thought it would be cool to have a discription that shows and hides on click at each row, I tried setting the row ID as a variable and include the toggle script in my while loop.

But it simply dont work as I want as the toggle button open the same row each time.

This is my code:

<div class="pInput">
<?php
$sql = "SELECT * FROM `pinput` WHERE Processnamn='$process'";
$result = mysqli_query($conn, $sql);
?>
<div class="divTable">
<div class="divTableRow">
<div class="divTableCell">Supplier</div> 
<div class="divTableCell">Supplier</div> 
<div class="divTableCell">Supplier</div> 
<div class="divTableCell"><button onclick='openaddInput()'><i class='fas fa-plus'></i></button></div>
</div>
<?php
if (mysqli_num_rows($result) > 0) {
    // output data of each row
        while ($row = mysqli_fetch_array($result)) {

            $rowID = $row['id'];?>
<script>
function toggle() {
  var x = document.getElementById("<?php echo $rowID ;?>");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}
</script>
        <div class="divTableRow">
            <div class="divTableCell"><?php echo $row['supplier'];?></div> 
            <div class="divTableCell"><?php echo $row['objekt'];?></div> 
            <div class="divTableCell"><button class="closeWindow" onclick="toggle()">?</button></div>
            <div class="divTableCell"><i class='fas fa-edit'></i></div>
        </div>
        <div class="beskr" id=<?php echo $rowID; ?>><?php echo $row['beskrivning'];?>
        </div>
    <?php    } 

   } else {
    echo "0 results";
} ?>
</div>  

Any help with this is much appreciated, maybe Im doing this all wrong, if so please point me in the right direction so I can learn to do it right.

Maybe I should do a function to generate the table since I need it for several boxes and just change some variables for it?

Thanks!

/Husky


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>