簡體   English   中英

如何為在JavaScript代碼中可訪問的PHP動態生成的按鈕唯一地設置ID

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

好吧,我有一個小問題。 因此,我將一個庫存系統連接到數據庫,並為數據庫中的每個項目在屏幕上生成一個按鈕,我試圖為每個按鈕提供一個可通過javascript訪問的php中的唯一ID。 生成按鈕的代碼如下所示

<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

>

我需要能夠訪問每個特定的唯一按鈕,以便能夠更改每個按鈕上的文本以及執行一些ajax來更改數據庫中的某些內容。

用以下內容替換您的條件:

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()'>";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM