[英]Copy button for table with row as id and value
I am trying to make a copy button. 我正在尝试复制按钮。 It only gives an error in Chrome: 它只会在Chrome中显示错误:
Uncaught SyntaxError: Unexpected token } 未捕获的SyntaxError:意外令牌}
I tried lots of different scripts, but none worked. 我尝试了许多不同的脚本,但没有一个起作用。 Everything works except the copy button. 一切正常,除了复制按钮。 I even went to the ICT part of our school. 我什至上过我们学校的ICT部分。 They did not have a solution. 他们没有解决方案。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
This is the copy script 这是复制脚本
<script>
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
</script>
This is the end of the copy script 复制脚本到此结束
<?php
$str1 = "'";
$str2 = '#';
?>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
<?php
$link = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sqlget = "SELECT FROM redeem";
$result = $link->query($sqlget);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo"<table>";
echo "<tr><th>Code: </th>
<th id=" . $row['code']. ">" . $row['code']. "</th>
<th>Expires: </th><th>". $row['expires']."</th>
<th><button onclick='copyToClipboard($str2".$row['code'].")'><p id=" . $row['code']. ">...</button></th></tr></table>";
}
} else {
echo "0 results";
}
?>
This code is the working version of the copy button. 此代码是复制按钮的工作版本。
<center>
<p id="p1">Hello, I'm TEXT 1</p>
<p id="p2">Hi, I'm the 2nd TEXT</p><br/>
<button onclick="copyToClipboard('#p1')">Copy TEXT 1</button>
<button onclick="copyToClipboard('#p2')">Copy TEXT 2</button>
<br/><br/><input class="textBox" type="text" id="" placeholder="Dont belive me?..TEST it here..;)" />
</center>
Hope you guys/girls can help me out. 希望你们能帮助我。
copyToClipboard($str2".$row['code'].")
produce : copyToClipboard(#code)
产生: copyToClipboard(#code)
It should be : 它应该是 :
"[...]copyToClipboard(\"$str2".$row['code']."\")[...]"
or even better : 甚至更好:
"onclick='copyToClipboard(" . '"' .$str2 . $row['code']. '"' . ")'"
With the output copyToClipboard("#code")
随着输出copyToClipboard("#code")
FIXED!!! 固定!!! IT finally woooorrrrkkssss!!! 它终于woooorrrrkkssss!
It took me a while. 我花了一段时间 But thanks to Blag his part of the code. 但是感谢Blag的一部分。 I found it. 我找到了。
<?php $str1 = "'"; ?>
This was the sollution. 这就是解决方案。
<th><button onclick='copyToClipboard(".$str1. $str2 . $row["code"] .$str1. ")'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.