douyujun0152 2017-07-19 03:26
浏览 255
已采纳

如何复制到剪贴板而不删除Textarea和文本复制

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条回答 默认 最新

  • dsirr48088 2017-07-19 03:44
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题