如何将 php 变量的值复制到用户的剪贴板

我已经尝试过将我上传的图像复制到用户的剪贴板,但图像正在上传,并且其地址在上传后显示,但我也需要将其地址复制到用户的剪贴板`


<?php

//upload.php

if($_FILES["file"]["name"] != '')

{

 $test = explode('.', $_FILES["file"]["name"]);

 $ext = end($test);

 $name = rand(100, 999) . '.' . $ext;

 $location = './upload/' . $name; 

 $url= 'http://i.com/upload/' . $name;



 move_uploaded_file($_FILES["file"]["tmp_name"], $location);

 echo '<img src="'.$location.'" height="150" width="225" class="img-thumbnail" />';




 echo "\n\n\n\n$url";




<!DOCTYPE html>

<html>

<body>


<p>Click on the button to copy the text from the text field. Try to paste the text </p>


<input type="text" value="$url" id="myInput">

<button onclick="myFunction()">Copy text</button>


<script>      function myFunction() 

{

  var copyText = document.getElementById("myInput");

  copyText.select();

  copyText.setSelectionRange(0, 99999)

  document.execCommand("copy");

  alert("Copied the text: " + copyText.value);

}

</script>

</body>


</html>

}

?>

`


Helenr
浏览 84回答 1
1回答

蛊毒传说

这应该适合你:<script>function myFunction() {&nbsp; &nbsp; let inputEl = document.getElementById("myInput");&nbsp; &nbsp; inputEl.select();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Select element&nbsp; &nbsp; inputEl.setSelectionRange(0, inputEl.value.length); // select from 0 to element length&nbsp; &nbsp; const successful = document.execCommand('copy');&nbsp; &nbsp;// copy input value, and store success if needed&nbsp; &nbsp; if(successful) {&nbsp; &nbsp; &nbsp; &nbsp; alert("Copied the text: " + inputEl.value);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; }}</script>编辑:澄清并修复其他一些小错误:<?php&nbsp; &nbsp; //upload.php&nbsp; &nbsp; if($_FILES["file"]["name"] != '')&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $test = explode('.', $_FILES["file"]["name"]);&nbsp; &nbsp; &nbsp; &nbsp; $ext = end($test);&nbsp; &nbsp; &nbsp; &nbsp; $name = rand(100, 999) . '.' . $ext;&nbsp; &nbsp; &nbsp; &nbsp; $location = './upload/' . $name;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $url= 'http://i.com/upload/' . $name;&nbsp; &nbsp; &nbsp; &nbsp; move_uploaded_file($_FILES["file"]["tmp_name"], $location);&nbsp; &nbsp; &nbsp; &nbsp; echo '<img src="'.$location.'" height="150" width="225" class="img-thumbnail" />';&nbsp; &nbsp; &nbsp; &nbsp; echo "\n\n\n\n$url";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $url = "";&nbsp; &nbsp; }?><!DOCTYPE html><html><head>&nbsp; &nbsp; <title>example title</title></head><body>&nbsp; &nbsp; <p>Click on the button to copy the text from the text field. Try to paste the text </p>&nbsp; &nbsp; <input type="text" value="<?php echo $url; ?>" id="myInput">&nbsp; &nbsp; <button onclick="myFunction()">Copy text</button>&nbsp; &nbsp; <script>&nbsp; &nbsp; function myFunction() {&nbsp; &nbsp; &nbsp; &nbsp; let inputEl = document.getElementById("myInput");&nbsp; &nbsp; &nbsp; &nbsp; inputEl.select();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Select element&nbsp; &nbsp; &nbsp; &nbsp; inputEl.setSelectionRange(0, inputEl.value.length); // select from 0 to element length&nbsp; &nbsp; &nbsp; &nbsp; const successful = document.execCommand('copy');&nbsp; &nbsp;// copy input value, and store success if needed&nbsp; &nbsp; &nbsp; &nbsp; if(successful) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Copied the text: " + inputEl.value);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5