while ($row = mysqli_fetch_array($result))

我有一些代码有问题。该程序允许我从一键复制一个在我的 BDDMysql 上寻找的密码。我有一个脚本,允许我<P>使用特定的 ID复制 html 标签的内容。所有这些我都在一个while(mysqli_fetch_array($result)). 所以问题是,当我单击复制一个密码时,只有我在剪贴板上复制了第一个 bdd。


<?php


$egest='SELECT * FROM gestion';

$result=mysqli_query($con,$egest);

while ($row = mysqli_fetch_array($result)){

//echo $row['subcat_nombre'];

?>


<script>

    function copyToClipboard(element) {

      var $temp = $("<input>");

      $("body").append($temp);

      $temp.val($(element).text()).select();

      document.execCommand("copy");

      $temp.remove();

    }

</script>


<table class="estilo-ps">

    <tr>

    <td colspan="3" class="td-tit"><b><?php echo $row['gest_nombre'] ?></b></td>

  </tr>

  <tr class="tr-borders">

    <th class="th-border-cent">Contrasenya</th>

  </tr>

  <tr class="tr-borders">

    <td class="td-border-cent">

        <center>

            <p hidden="hidden" id="p1"><?php echo $row['gest_contra']; ?></p><br>

            <p>clic per copiar la contrasenya</p>

            <img src="img/key.png" class="copy" onclick="copyToClipboard('#p1')"/>

        </center>

    </td>

 </tr>

<?php

}

?>


素胚勾勒不出你
浏览 414回答 2
2回答

当年话下

您不能有重复的 ID。使用类而不是 ID。然后使用适当的 DOM 选择函数在单击的元素旁边找到具有该类的元素。此外,将函数从循环中取出,不需要为每一行重新定义。<script>&nbsp; &nbsp; function copyToClipboard(img) {&nbsp; &nbsp; &nbsp; var $element = $(img).siblings(".p1");&nbsp; &nbsp; &nbsp; var $temp = $("<input>");&nbsp; &nbsp; &nbsp; $("body").append($temp);&nbsp; &nbsp; &nbsp; $temp.val($element.text()).select();&nbsp; &nbsp; &nbsp; document.execCommand("copy");&nbsp; &nbsp; &nbsp; $temp.remove();&nbsp; &nbsp; }</script><?php$egest='SELECT * FROM gestion';$result=mysqli_query($con,$egest);while ($row = mysqli_fetch_array($result)){//echo $row['subcat_nombre'];?><table class="estilo-ps">&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td colspan="3" class="td-tit"><b><?php echo $row['gest_nombre'] ?></b></td>&nbsp; </tr>&nbsp; <tr class="tr-borders">&nbsp; &nbsp; <th class="th-border-cent">Contrasenya</th>&nbsp; </tr>&nbsp; <tr class="tr-borders">&nbsp; &nbsp; <td class="td-border-cent">&nbsp; &nbsp; &nbsp; &nbsp; <center>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p hidden="hidden" class="p1"><?php echo $row['gest_contra']; ?></p><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>clic per copiar la contrasenya</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="img/key.png" class="copy" onclick="copyToClipboard(this)"/>&nbsp; &nbsp; &nbsp; &nbsp; </center>&nbsp; &nbsp; </td>&nbsp;</tr></table><?php}?>

幕布斯6054654

<?php$egest='SELECT * FROM gestion';$result=mysqli_query($con,$egest);?><script>&nbsp; &nbsp; function copyToClipboard(element) {&nbsp; &nbsp; &nbsp; var $temp = $("<input>");&nbsp; &nbsp; &nbsp; $("body").append($temp);&nbsp; &nbsp; &nbsp; $temp.val($(element).text()).select();&nbsp; &nbsp; &nbsp; document.execCommand("copy");&nbsp; &nbsp; &nbsp; $temp.remove();&nbsp; &nbsp; }</script><table class="estilo-ps">while ($row = mysqli_fetch_array($result)){//echo $row['subcat_nombre'];?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; <td colspan="3" class="td-tit"><b><?php echo $row['gest_nombre'] ?></b></td>&nbsp; </tr>&nbsp; <tr class="tr-borders">&nbsp; &nbsp; <th class="th-border-cent">Contrasenya</th>&nbsp; </tr>&nbsp; <tr class="tr-borders">&nbsp; &nbsp; <td class="td-border-cent">&nbsp; &nbsp; &nbsp; &nbsp; <center>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p hidden="hidden" id="p1"><?php echo $row['gest_contra']; ?></p><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>clic per copiar la contrasenya</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="img/key.png" class="copy" onclick="copyToClipboard('#p1')"/>&nbsp; &nbsp; &nbsp; &nbsp; </center>&nbsp; &nbsp; </td>&nbsp;</tr><?php}?></table>您正在尝试循环脚本。脚本应该只声明一次。将您的 while 循环移到 之后<table>。
打开App,查看更多内容
随时随地看视频慕课网APP