Thursday, March 6, 2014

Facebook like Profile Tooltip with jQuery, Ajax, Php & Mysql

In this post i am going to tell u guz how to create a facebook like profile tooltip with jQuery php & mysql. Probably this will be my last post till 26 march 2014 because currently my paper are going on and i get very less time for my girlfriend (computer) so we will meet after 26 march 2014 byeeeeeee
facebook like profile tooltip with jQuery and php

Live Demo Download


first we are creating a database with general info id, fullname, headline, avatar, cover
CREATE TABLE IF NOT EXISTS `fb_profile_tooltip` (
  `fullname` varchar(255) NOT NULL,
  `headline` varchar(255) NOT NULL,
  `avatar` text NOT NULL,
  `cover` text NOT NULL,
  PRIMARY KEY (`id`)


Html is having nothing special we just have html5 data-id (anchor tag) with store the user id and that id will be fetched by jQuery and send to php to create a dynamic profile tooltip & a div with a class p-tooltip
 <div class="p-tooltip"></div>
 <table cellpadding="20">
  <td><a href="#" class="profile" data-id="1">user1</a></td>
  <td><a href="#" class="profile" data-id="2">user2</a></td>
  <td><a href="#" class="profile" data-id="3">user3</a></td>
  <td><a href="#" class="profile" data-id="4">user4</a></td>


font-family: verdana;
text-decoration: none;
background: #fafbfb;
border: 1px solid #BFBFBF;
width: 320px;
margin: 0 auto;
border-radius: 5px;
position: absolute;
display: none;
padding: 0 0 10px 0;
.p-tooltip-cover img{
width: 100%;
height: 120px;
text-align: left;
margin-top: -45px;
margin-left: 10px;
.p-tooltip-avatar img{
height: 75px;
width: 75px;
padding: 4px;
background: #fff;
border: 1px solid #ccc;
cursor: pointer;
text-align: left;
.p-tooltip-info .p-username{
float: none;
margin-top: -70px;
margin-left: 100px;
font-size: 14px;
font-weight: bold;
color: white;
.p-tooltip-info .p-headline{
float: none;
margin-top: 6px;
margin-left: 100px;
font-size: 12px;
color: black;
float: right;
margin-top: 5px;
.p-tooltip-button button{
cursor: pointer;

we have created two function 1> showProfileTooltip(); 2> hideProfileTooltip(); when user mouseover a link we use the showProfileTooltip(); this function get the data from a get_profile.php and feed the data in the p-tooltip div and when the mouseleave we use hideProfileTooltip();
<script src=""></script>
 <script type="text/javascript">
  function showProfileTooltip(e, id){
   var top = e.clientY + 20;
   var left = e.clientX - 10;

   //send id & get info from get_profile.php
    url: 'get_profile.php?id='+id,
    beforeSend: function(){
    success: function(html){

  function hideProfileTooltip(){

   var id = $(this).attr('data-id');
   showProfileTooltip(e, id);



in this page we are getting the user id and generating the content based on its uniquer id fetched from the html5 data-id anchor tag
 //include '../connect.php';
 ///connect to mysql 
 mysql_connect('localhost','root', '');
 //get the user id
 if(isset($_GET['id']) && !empty($_GET['id'])){
  $id = mysql_real_escape_string($_GET['id']);
  //get info of user based on user id
  $q = mysql_query("SELECT * FROM fb_profile_tooltip WHERE id='$id'");
  if(mysql_num_rows($q) == 1){
   $row = mysql_fetch_assoc($q);
   //output the html
  <div class="p-tooltip-cover">
   <img src="<?php echo $row['cover'];?>">
  <div class="p-tooltip-avatar">
   <img src="<?php echo $row['avatar'];?>" />
  <div class="p-tooltip-info">
   <a target="_blank" href="" style="text-decoration:none;">
    <div class="p-username"><?php echo $row['fullname'] ?></div>
    <div class="p-headline"><?php echo $row['headline'] ?></div>
  <div class="p-tooltip-button">
   <a target="_blank" href="">
    <button>Add as friend</button><button>message</button>
   echo "Error";