关于JS Click的PHP函数?

时间:2014-02-04 14:57:27

标签: javascript php jquery twitter-bootstrap

我有一个按钮,可以将链接复制到剪贴板,但是已经使用bit.ly缩短了。在那一刻,我有它,以便它在页面加载时创建bit.ly链接,这看起来有点乱,所以我只是想知道我怎么能只在复制到剪贴板时才这样做?

var client = new ZeroClipboard( document.getElementById("copy-link"), {
moviePath: "ZeroClipboard.swf"
} );

client.on( "load", function(client) {
// alert( "movie is loaded" );

    client.on( "complete", function(client, args) {
        // `this` is the element that was clicked
        $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'Link Copied',
            // (string | mandatory) the text inside the notification
            text: 'Link has been copied to your clipboard'
        });
        $('[data-toggle="dropdown"]').parent().removeClass('open');
    } );
} );

这是我的PHP

<?php

$url = Router::url(null, true);
if ($this->UserAuth->isLogged()) {
    $url .= '?shareref=' . $var['User']['id'] . '';
}
$shortlink = bitly_v3_shorten($url, 'bit.ly');

?>

2 个答案:

答案 0 :(得分:2)

JQuery Ajax将是您的最佳选择:http://api.jquery.com/jquery.ajax/ - 创建它以便在您单击按钮时运行PHP。

答案 1 :(得分:0)

Javascript可以是这样的

// Java script

jQuery("copyButton").click(function() {

    jQuery.ajax({
         type : "post",
         dataType : "json",
         url : 'urlshortener.php',
         success: function(response) {
            if(response.type == "success") {
                var shorturl = response.short;
            }
         }
    });
});  

PHP看起来像这样,也许通过XHTTP裁判等保护它是好事。

// PHP 

$url = Router::url(null, true);
if ($this->UserAuth->isLogged()) {
    $url .= '?shareref=' . $var['User']['id'] . '';
}
$shortlink = bitly_v3_shorten($url, 'bit.ly');


$result['type']= "success";
$result['short'] = $url;
$result = json_encode($result);
echo $result;
die();