为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Helenr 2023-10-17 17:02:50
我已经尝试过将我上传的图像复制到用户的剪贴板,但图像正在上传,并且其地址在上传后显示,但我也需要将其地址复制到用户的剪贴板`<?php//upload.phpif($_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>}?>`
查看完整描述

1 回答

?
蛊毒传说

TA贡献1895条经验 获得超3个赞

这应该适合你:


<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>


查看完整回答
反对 回复 2023-10-17
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信