0

I have a button that will copy a link to the clipboard but it has been shortened using bit.ly. At the minute I have it so that it creates the bit.ly link on page load which seems a bit messy so I just wondered how I can make it only do it when it copies to the clipboard?

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');
    } );
} );

This is my PHP

<?php

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

?>

2 Answers 2

2

JQuery Ajax will be your best option: http://api.jquery.com/jquery.ajax/ - Create it so that it runs the PHP when you click the button.

Sign up to request clarification or add additional context in comments.

2 Comments

Yup. Ajax; ajax all the way :)
Perhaps you can add to your answer a wee bit to provide some sample code and direction. As his PHP is currently written, a variable will be assigned, but then nothing else. He would want to either echo $shortLink for a raw response, or enclose it in some json structure and return that in the response.
0

Javascript can be like this

// 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 looks like this, maybe its good to be protect it by XHTTP referees etc..

// 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();

2 Comments

Thanks, but how is the variable passed through of the url?
The example above don't need variable to post to php (url created on php i guess at least i thought so... if you need to pass var to php via jquery, in jQuery.ajax definition you need to add data : {variable:value} , data is the name of the parameter you send to php, value is the value of it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.