Home > Back-end >  Is there a way to make the copy to clipboard feature copy the correct row instead of always copying
Is there a way to make the copy to clipboard feature copy the correct row instead of always copying

Time:11-11

The copy to clipboard feature with a button works if it is only has exact variable however if it is a echo'd variable which is different on each row then it only copys the first row no matter which button you click. I have tried many different methods and they all seem to do the same. Is there something which I can add in the code which would make the correct row button copy the correct row $dir ?

function copy(element_id) {
  var aux = document.createElement("div");
  aux.setAttribute("contentEditable", true);
  aux.innerHTML = document.getElementById(element_id).innerHTML;
  aux.setAttribute("onfocus", "document.execCommand('selectAll',false,null)");
  document.body.appendChild(aux);
  aux.focus();
  document.execCommand("copy");
  document.body.removeChild(aux);
}
<p id="demo">
  <?php echo $dir ?>
</p>
<button onclick="copy('demo')">Copy Keeping Format</button> <br><br>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Code:

$numresults=$info["count"];
echo"<div style='position: fixed; float: right; padding-left: 450px;'><a class=clear href=javascript:history.go(-1)>Search again</a></div>";
echo "<div><p>There are <b>$numresults</b> results for your search '<i><b>$SearchFor</i></b>'";
if ($numresults>0){

    echo " these are:</p></div>";
    echo "<div>";
    

    for ($x=0; $x<$numresults; $x  ) {                          //display the results
        $sam=$info[$x]['samaccountname'][0];
        $disp=$info[$x]['displayname'][0];
        $dir=$info[$x]['homedirectory'][0];
        $fil=$info[$x]['homedirectory'] [0];
        $displayout=substr($sam, 0, 4);
                
        echo "User Name &nbsp;: $sam";
        echo "<br>Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : $disp";
        echo "<br>Home Drive : <a class=clear href=$dir>$dir</a><br>";?>
         <p id="demo<?php echo $i; ?>">
       <?php echo $dir ?>
        </p>
       <button onclick="copy('demo<?php echo $i; ?>')">Copy Keeping Format</button> <br><br>

<script>
function copyTo(input){
    input.select();
    document.execCommand("copy");
}
</script>

CodePudding user response:

If <p id="demo"><?php echo $dir ?></p> can repeat many times in the code (due to being contained within a loop) then obviously you can't identify it using a single id, because - by definition - an ID must be unique, and that's not the case here.

You'd need to generate a different ID for each <p> element.

In the simplest case you can just use PHP to add a counter to the ID.

First, outside your loop, initialise a counter:

$i = 0;

Then, within the loop, use that to make a unique element ID:

<p id="demo<?php echo $i; ?>">
  <?php echo $dir ?>
</p>
<button onclick="copy('demo<?php echo $i; ?>')">Copy Keeping Format</button> <br><br>

Finally, before your loop ends, increment the counter:

$i  ;
  • Related