skip to Main Content

I have small Copy to clipboard script that copy the text in textarea.

Now, i have several question:

  1. In a normal html page- after i click “Copy Text”, its working perfect. But, in a PHP page with PHP Variables the text actually copied but the textarea dissappear. Why its happening and how do i make it stay after copy?
    Also, after the text copied, the text “jumps” to the address bar like that:

enter image description here

  1. Is there a way to copy from div or paragraph instead of textarea?
  2. Related to the previous question: why it shows the code in the textarea instead of a link?

Here’s the code at the HTML page:

<head>
<script type="text/javascript" src="http://coreneto.com/delete/generator/copy/js/jquery-latest.min.js"></script>

<script language="Javascript">
var copytoclip=1
function HighlightAll(theField) {
    var tempval=eval("document."+theField)
    tempval.focus()
    tempval.select()
    if (document.all&&copytoclip==1){
        therange=tempval.createTextRange()
        therange.execCommand("Copy")
        window.status="Contents highlighted and copied to clipboard!"
        setTimeout("window.status=''",1800)
    }
}
</script>

</head>

<body>
    <textarea id="p1" name="select1" rows=3 cols=75 style="">
    <a href="google.com">Google</a>
</textarea>

<button class="button-main" style="max-width:200px;" onclick="copyToClipboard('#p1')">Copy Text</button>
<script type="text/javascript">    
function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
  $temp.remove();
} 
    </script>

Here’s the code at the PHP page:

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Ebay Shortner</title>
    <meta name="description" content="Welcome to ebay shortner. Best ebay shortner EVER">
    <link rel="stylesheet" href="./all/style.css" type="text/css">
    <link rel="stylesheet" href="./all/screen.css" type="text/css">
<script type="text/javascript" src="http://coreneto.com/delete/generator/copy/js/jquery-latest.min.js"></script>

        <script language="Javascript">
var copytoclip=1
function HighlightAll(theField) {
    var tempval=eval("document."+theField)
    tempval.focus()
    tempval.select()
    if (document.all&&copytoclip==1){
        therange=tempval.createTextRange()
        therange.execCommand("Copy")
        window.status="Contents highlighted and copied to clipboard!"
        setTimeout("window.status=''",1800)
    }
}
</script>

</head>
<body>
<center>
<?php
if(isset($_POST['submit'])){
$url = $_POST['url'];
$name = array($url);
foreach ($name as $name) {
    if (preg_match("/^[.<[#`]/",$url)) {
        echo "<br><center><font class="error">Use only english leeters</center>";  
        Die();
    }
if (preg_match("/א|ב|ג|ד|ה|ו|ז|ח|ט|י|כ|ל|מ|נ|ס|ע|פ|צ|ק|ר|ש|ת|ם|ף|ץ|ן/",$url)) {
echo "<br><center><font class="error">Use only english letters</center>";  
 Die();
}
    if (!strlen($url)) {
       echo "<br><center><font class="error">empty filed</center>";
       Die();
    }
    if (strlen($url) > 700) {
       echo "<br><center><font class="error">That was very long. Please short it a bit</center>";
       Die();
    }
}


if (count(explode('ebay.com',$url))>1) {
    $ebay_url = "http://rover.ebay.com/rover/1/711-53200-19255-0/1?ff3=4&pub=5575165347&toolid=10001&campid=5337851510&customid=&mpre=".urlencode($url)."";
}
else{
    $ebay_url = "http://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=10&pub=5575165347&toolid=10001&campid=5337851510&customid=&icep_uq=".urlencode($url)."&icep_sellerId=&icep_ex_kw=&icep_sortBy=15&icep_catId=&icep_minPrice=&icep_maxPrice=&ipn=psmain&icep_vectorid=229466&kwid=902099&mtid=824&kw=lg";
}

$token = "token";
$endpoint = "https://api-ssl.bitly.com/v3/shorten?access_token=".$token."&longUrl=".urlencode($ebay_url);
$json = json_decode(file_get_contents($endpoint), true);
echo $ebay_link = $json["data"]["url"];
echo '<br>';


if (count(explode('amazon.com/',$url))>1) {
$ebay_url = "".urlencode($url)."ref=as_li_ss_tl?encoding=UTF8&amp;tag=16684-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325";
}
else{ 

$ebay_url = "https://www.amazon.com/s/ref=as_li_ss_tl?field-keywords=".urlencode($url)."&linkCode=ll2&tag=16684-20&linkId=c333024455a04f66e02172bdda2a4338";
}

$endpoint = "https://api-ssl.bitly.com/v3/shorten?access_token=".$token."&longUrl=".urlencode($ebay_url);
$json = json_decode(file_get_contents($endpoint), true);
echo $amazon = $json["data"]["url"];    

?>
<br>
<center>

<form name="vini">
<a class="highlighttext" style="font-family:arial; font-size:13px;" href="javascript:HighlightAll('vini.select1')">select all</a><br>
<textarea id="p1" name="select1" rows=2 cols=75 style="font-family:tahoma;color:#555;border:1px dashed #ccc">
<?php echo $short_url ;?> 
</textarea>
<button class="button-main" style="max-width:200px;" onclick="copyToClipboard('#p1')">Copy text</button>   
<script type="text/javascript">    
function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
}    
    </script>

</form>

<?php
}
?>
</center>  
            </ul>
        </div>  
</div>

Obvious it is related to the PHP code, but what is it?

Here’s the HTML page: HERE
And here’s live PHP page: HERE

2

Answers


  1. Chosen as BEST ANSWER

    Alexander you were right! Did that:

    <div name="vini">
    <a class="highlighttext" style="font-family:arial; font-size:13px;" href="javascript:HighlightAll('vini.select1')">select all</a><br>
    <textarea id="p1" name="select1" rows=2 cols=75 style="font-family:tahoma;color:#555;border:1px dashed #ccc">
    <?php echo $ebay_link ;?>
    
    <?php echo $amazon ;?> 
    </textarea>
    
    </div>
    
    <button class="button-main" style="max-width:200px;" onclick="copyToClipboard('#p1')">Copy text</button>   
    <script type="text/javascript">    
    function copyToClipboard(element) {
      var $temp = $("<input>");
      $("body").append($temp);
      $temp.val($(element).text()).select();
      document.execCommand("copy");
    }    
        </script>
    

    And everything works fine.


  2. Your issue is that you have your button inside of a form tag on the PHP page. The form doesn’t have an METHOD attribute so by default it perform a GET request when submitted.

    With a GET request a form will append element names and their values to the querystring and direct the browser to the url specified in the action attribute. If a form doesn’t have an action the GET request is performed against the current url.

    <form name="vini">
    <a class="highlighttext" style="font-family:arial; font-size:13px;" href="javascript:HighlightAll('vini.select1')">select all</a><br>
    <textarea id="p1" name="select1" rows=2 cols=75 style="font-family:tahoma;color:#555;border:1px dashed #ccc">
    
    </textarea>
    <button class="button-main" style="max-width:200px;" onclick="copyToClipboard('#p1')">Copy text</button>   
    <script type="text/javascript">    
    function copyToClipboard(element) {
      var $temp = $("<input>");
      $("body").append($temp);
      $temp.val($(element).text()).select();
      document.execCommand("copy");
    }    
        </script>
    
    </form>
    

    For example, when the textarea with the name select1 has the value of iphone8 in the form above, clicking the button causes the browser to navigate to index.php?select1=iphone8

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search