0

I have a SQL-query in a WHILE loop that creates multiple table rows. One cell has an input field with an id named after sql row ($row2['ticketid']).

I also have a button being created at the end of each row which is supposed to copy the text from the input field.

My javascript-code copies only the last instance of the row. Putting it outside the loop makes it copy only the first instance..

<script>
    function myFunction() {

      /* Get the text field */
      var copyText = document.getElementById('<? echo $row2['ticketid'];?>');

      /* Select the text field */
      copyText.select();
      copyText.setSelectionRange(0, 99999); /*For mobile devices*/

     /* Copy the text inside the text field */
      document.execCommand("copy");

      /* Alert the copied text */
      alert("Ticket-ID kopiert: " + copyText.value);
    }
    </script>

I want each button to copy the text from the input field from the same row to which the button belongs.

This is my code so far:

    <table class="some_style" style="font-size:15pt;border-collapse: collapse;overflow:hidden;">
                <tr>
                    <th style="width:70px;border-bottom:1px solid silver;text-align:center;color:black;">
                        <font size="2">Art:</font>
                    </th>                       
                    <th style="width:300px;border-bottom:1px solid silver;text-align:center;color:black;">
                    </th>

                    <th style="width:300px;border-bottom:1px solid silver;text-align:left;color:black;">
                    <font size="2">Ticket-ID:</font>
                    </th>

                    <th style="width:100px;border-bottom:1px solid silver;color:black;">
                    <font size="2">Status:</font>
                    </th>
                    <th style="width:80px;border-bottom:1px solid silver;color:black;">
                    <font size="2">Aktion:</font>
                    </th>
                </tr>
                <?

                $sql2="SELECT * FROM tickets WHERE user='$login'";
                $result2 = mysqli_query($con,$sql2);   
                $y=0;
                $helper2=array();

                while($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {

                $str="";
                if ($row2['itemid'] == 1) {$str="Ticket";}
                if ($row2['itemid'] == 2) {$str="Rotes Ticket";}
                if ($row2['itemid'] == 3) {$str="Blaues Ticket";}
                if ($row2['itemid'] == 4) {$str="Billet";}

                $str2="";
                if ($row2['red'] == 0) {$str2="Nicht eingelöst";}
                if ($row2['red'] == 1) {$str2="Eingelöst";}

                ?>
                <tr>
                    <th style="width:70;text-align:left;background-color:snow;">
                    <font size="2" style="font-weight:normal;text-align:left;background-color:snow;"><img src="app/tickets/<? echo $row2['itemid']; ?>.png" width="50" height="41"></font>
                    </th>

                    <th style="width:300px;text-align:left;font-size:14pt;background-color:snow;padding-left:5px;">
                    <font style="font-weight:normal;"><? echo $str; ?></font>
                    </th>

                    <th style="width:300px;background-color:snow;text-align:left;">
                    <font style="font-weight:normal;font-size:14pt;"><input style="width:300px;background-color:snow;border:0px;text-align:left;" type="text" readonly value="<? echo $row2['ticketid']; // etc.. ?>" id="<? echo $row2['ticketid'];?>"></font>
                    </th>
                <th style="width:200px;background-color:snow;">
                <font style="font-weight:normal;text-align:left;font-size:10pt;"><? echo $str2; // etc.. ?></font></th>
                                        <th style="width:80px;color:black;">
                    <button onclick="myFunction()" style="width:80px;font-size:10pt;background-color:white;border-radius:5px;height:30px;border:1px solid black;" onmouseover='this.style.backgroundColor = "silver"' onmouseout='this.style.backgroundColor = "white"' >Kopieren</button>

                    </th><script>
function myFunction() {

  /* Get the text field */
  var copyText = document.getElementById('<? echo $row2['ticketid'];?>');

  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999); /*For mobile devices*/

  /* Copy the text inside the text field */
  document.execCommand("copy");

  /* Alert the copied text */
  alert("Ticket-ID kopiert: " + copyText.value);
}
</script>
                <? 
                $y++;
                }
                ?>

                </table>
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
matt2k12
  • 23
  • 5
  • {var elemval = ""; var copyText = document.getElementById( elemval);} can u check this? – Rasa Mohamed Apr 25 '20 at 09:26
  • This is purely a client-side job, you can't mix PHP with this. You should ask a new question, include an example of the __browser-rendered markup__ of the table, and explain in details what you want to do. – Teemu Apr 25 '20 at 09:34
  • Rada Mohamed thanks for the reply, i checked but this doesnt work. – matt2k12 Apr 25 '20 at 16:39
  • OK i found the solution, it is very easy, in case someone is interested. I just change the name of the function to " function myFunction echo $row2['ticketid'];?>() {" ... the point is, before i had one function being created, which was overwritten everytime the loop started anew. – matt2k12 Apr 25 '20 at 17:04

0 Answers0