Search code examples
javascriptphphtmlimageclipboard

How can i copy value of my php variable to user's clipboard


i have tried this one to copy my uploaded image to user's clipborad but image is uploading and its adress is showing after its uploading but i need to copy its adress to the user's clipboard too `

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

Solution

  • This should work for you:

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

    Edit: To clarify and fix some other small errors:

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