Tuesday, 26 May 2015

Facebook SDK javascript - Facebook Login Check, Get Login Details, Share URL and POST to Profile

Facebook SDK javascript - Facebook Login Check, Get Login Details, Share URL and POST to Profile

Add following javascript code which are Responsible for load javascript SDK.
window.fbAsyncInit = function() {
    FB.init({
        appId      : FB_APP_ID, 
        cookie     : true,
        xfbml      : true,                    
        version    : 'v2.3'
    });

}; 
(function(d){
    var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    d.getElementsByTagName('head')[0].appendChild(js);
}(document));
Please don't forget to replace the "FB_APP_ID" with  facebook API Id.


Add following javascript code which will leverage the functionality like Login check, login details, Share URL and Post to facebook.
function fbLogin(){
    FB.getLoginStatus(function(response) {
     if (response.status === 'connected') {
       console.log('You are login in FB.');
     }
     else {
       FB.login();
     }
   });

}
function fbLoginDetails(){                
    FB.api('/v2.2/me?fields=name,email,location,gender,birthday', function(response) {                   
        console.log(response.toSource());                    
    });
}

function fbShareURL(){
    FB.ui(
        {
         method: 'share',
         href: 'http://www.web-technology-experts-notes.in/2013/09/Facebook-Registration-Plugin-with-Custom-Fields.html'
       }, function(response){   }); 
}

function fbPostURL(){
    FB.ui({
        method: 'share_open_graph',
        action_type: 'og.likes',
        action_properties: JSON.stringify({
         object:'http://www.web-technology-experts-notes.in/2013/09/Facebook-Registration-Plugin-with-Custom-Fields.html',
        })
       }, function(){})

}


Add following HTML code which will create button on which click functionality will work.
<a class="button-facebook" href="javascript:fbLogin()">Facebook Login Check</a>
        <a class="button-facebook" href="javascript:fbLoginDetails()">Facebook Get Login Details</a>

        <a class="button-facebook" href="javascript:fbShareURL()">Facebook Share URL</a>
        <a class="button-facebook" href="javascript:fbPostURL()">Facebook POST URL</a>





Monday, 25 May 2015

How can I upload file asynchronously in php?

How can I upload file asynchronously in php?

Following are simple 4 Steps to upload file asynchronously  in PHP.

Step #1
Add jQuery javascript file in your page - Need for Ajax call.


<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>


Step #2
Add Following Html, where you want to show the button to upload the file.






Step #3:
Add following javascript code which is responsible for uploading the file.
URL: /folder/upload-file.php
Description: Here you need to write the send the request to server.
$( document ).ready(function() {
    $(':button').click(function(){
        var formData = new FormData($('form')[0]);
        $.ajax({
            url: '/folder/upload-file.php', //Location of upload file 
            type: 'POST',
            xhr: function() {  // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                return myXhr;
            },

            beforeSend: function(data){},
            success: function(data){},
            error: function(data){},        
            data: formData,        
            cache: false,
            contentType: false,
            processData: false
        });
    });
});


Step #4
Add Following code in "/folder/upload-file.php" which will upload the file in Server.
$targetFile="/path/to/upload/file";//Location of srver
if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) {
        echo "success";
    } else {
        echo "failure";
    }