【问题标题】:How can i copy value of my php variable to user's clipboard如何将我的 php 变量的值复制到用户的剪贴板
【发布时间】:2020-03-30 06:31:56
【问题描述】:

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

<?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>
}
?>
`

【问题讨论】:

    标签: javascript php html image clipboard


    【解决方案1】:

    这应该适合你:

    <script>
    function myFunction() {
        let inputEl = document.getElementById("myInput");
        inputEl.select();                                    // Select element
        inputEl.setSelectionRange(0, inputEl.value.length); // select from 0 to element length
    
        const successful = document.execCommand('copy');   // copy input value, and store success if needed
    
        if(successful) {
            alert("Copied the text: " + inputEl.value);
        } else {
            // ...
        }
    }
    </script>
    

    编辑:澄清和修正其他一些小错误:

    <?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";
        } else {
            $url = "";
        }
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>example title</title>
    </head>
    
    <body>
        <p>Click on the button to copy the text from the text field. Try to paste the text </p>
    
        <input type="text" value="<?php echo $url; ?>" id="myInput">
        <button onclick="myFunction()">Copy text</button>
    
        <script>
        function myFunction() {
            let inputEl = document.getElementById("myInput");
            inputEl.select();                                    // Select element
            inputEl.setSelectionRange(0, inputEl.value.length); // select from 0 to element length
    
            const successful = document.execCommand('copy');   // copy input value, and store success if needed
    
            if(successful) {
                alert("Copied the text: " + inputEl.value);
            } else {
                // ...
            }
        }
        </script>
    </body>
    </html>
    

    【讨论】:

    • 在哪里添加这个
    • @Animesh 我编辑以包含脚本标签和功能。该代码属于函数myFunction()
    • @ ivan86 okhh 我也在检查它我试过你以前的代码 frnd
    • @Animesh 没问题,让我知道它是否有效(我有 99.9% 的把握它会)。
    • 它也不起作用我也尝试过添加你的脚本,但现在它甚至没有上传我的 img
    猜你喜欢
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 2018-04-15
    • 2020-07-11
    相关资源
    最近更新 更多