0

I have a problem with some code. The proram allows me to copy from one lick one password that is sought on my BDDMysql. I have a script that allows me to copy the content of the html tag <P>, with a specific ID. All of that i got it inside of a while(mysqli_fetch_array($result)). So the problem is, when i click for copy one password, only i get the first one of the bdd copied on the clipboard.

<?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
}
?>
3
  • It doesn't make sense to define a function in a loop. A function can only have one definition, you're just replacing it each time. Commented May 24, 2019 at 9:21
  • IDs have to be unique, you can't have multiple id="p1". $(element) will always return the first one, not the one in the same row that the user clicked on. Commented May 24, 2019 at 9:24
  • You have a <table> tag but no </table>. Do you really want to create a separate table for each row that the query returns? Commented May 24, 2019 at 9:24

2 Answers 2

1

You can't have duplicate IDs. Use a class instead of an ID. Then use the appropriate DOM selection function to find the element with that class next to the clicked element.

Also, take the function out of the loop, it doesn't need to be redefined for each row.

<script>
    function copyToClipboard(img) {
      var $element = $(img).siblings(".p1");
      var $temp = $("<input>");
      $("body").append($temp);
      $temp.val($element.text()).select();
      document.execCommand("copy");
      $temp.remove();
    }
</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">
    <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" class="p1"><?php echo $row['gest_contra']; ?></p><br>
            <p>clic per copiar la contrasenya</p>
            <img src="img/key.png" class="copy" onclick="copyToClipboard(this)"/>
        </center>
    </td>
 </tr>
</table>
<?php
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Ok taking notes for the next time. Thx a lot!
0
<?php

$egest='SELECT * FROM gestion';
$result=mysqli_query($con,$egest);
?>

<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">
while ($row = mysqli_fetch_array($result)){
//echo $row['subcat_nombre'];
?>
    <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
}
?>
</table>

You are trying to loop the script. Script should be declared only once.
Move your while loop after <table> .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.