lionetti12345
lionetti12345

Reputation: 7

How to uniquely set id's to buttons generated dynamically in php that are accessible in javascript code

Okay so I'm having a small problem. So I have an inventory system connected to a database and for each item in the database a button is generated on screen and I'm trying to give each button a unique Id in php that I can access through javascript. the code for generating the buttons looks like this

<table class = "table">
<thead>
    <tr>
        <td><center><strong>Item Id</strong></center> </td>
        <td><center><strong>Item Description</strong></center> </td>
        <td><center><strong>Item Type</strong> </center></td>
        <td><center><strong>Availability</strong></center></td>
        <td><center><strong>Name</strong></center></td>
        <td><center><strong>Check in/out button</strong></center></td>
    </tr>
</thead>
<tbody>
<?php
    mysql_select_db('inventory management system');
    $query="SELECT * FROM inventory";
    $results = mysql_query($query);
    $a = 0;

    while($row = mysql_fetch_array($results)) {
      $a++;
      echo '<script type = "text/javascript">c++;</script>'
    ?>
        <tr>
            <td><center><?php echo $row['item_id']?></center></td>
            <td><center><?php echo $row['item_desc']?></center></td>
            <td><center><?php echo $row['item_type']?></center></td>
            <td><center><?php echo $row['availability_status']?></center></td>
            <td><center><?php echo $row['name']?></center></td>
            <td><center><?php
            if(is_null($row['name'])){//generates the buttons whether or not an item is signed in or out
              echo "<input class='btn btn-default' type='submit' value='Check Out'  id ='$a' onclick='updateinventory()'>";//how can I get a unique id here ?
            }else if(!is_null($row['name'])){
              $lol = "<?php <script type = 'text/javascript'> x++; </script> ?>";
              echo "<input class='btn btn-default' type='submit' value='Check In'  id ='$a' onclick='checkingitemin()'>";

            }


             ?></center></td>
        </tr>

    <?php
    }
    ?>
    </tbody>
    </table

>

i need to be able to access each specific unique button to be able to change the text on each button as well as execute some ajax to change some stuff in the database.

Upvotes: 0

Views: 47

Answers (1)

Max
Max

Reputation: 891

Replace your conditions with this :

if(is_null($row['name'])) {//generates the buttons whether or not an item is signed in or out
    echo "<input class='btn btn-default' type='submit' value='Check Out' id='".$row['item_id']."' onclick='updateinventory()'>";
} else { // You don't need another `if` statement here
    // I don't think the next instruction is usefull :)
    // $lol = "<?php <script type = 'text/javascript'> x++; </script>";
    echo "<input class='btn btn-default' type='submit' value='Check In' id='".$row['item_id']."' onclick='checkingitemin()'>";
}

Upvotes: 1

Related Questions