title: How to code like button in php using ajax?
body: <p>I want a like button coding like Facebook. I tried it but the problem is that the data return from other PHP file through AJAX doesn't get displayed. Here is my code.</p>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>CREATE TABLE `imagedata` (
   `id` int(10) DEFAULT NULL,
   `image` varchar(1000) DEFAULT NULL,
   `upload_by` int(10) DEFAULT NULL
  )
  CREATE TABLE `likedata` (
   `liked_by` int(10) DEFAULT NULL,
   `image_id` int(10) DEFAULT NULL
  )
   CREATE TABLE `userdata` (
    `id` int(2) NOT NULL,
    `names` varchar(10) NOT NULL
   )
  </code></pre>
  
  <p><strong>db.php</strong></p>
  
  <pre><code> $cn=mysql_connect("localhost","root","");
  if(!$cn){
      echo "Database problem occur.";
      die();
  }
  $db=mysql_select_db("test",$cn);
  if(!$db){
      echo "Database not exist.";
      die();
  }
  function getLike($tablename,$image_id){
      $num=0;
      $result=mysql_query("select liked_by from $tablename where image_id=$image_id");
      $num=mysql_num_rows($result);
      return $num;
  }
  function getCheck($tablename,$id,$image_id){
      $n=0;
      $result=mysql_query("select * from $tablename where liked_by=$id and image_id=$image_id");
      $n=mysql_num_rows($result);
      return $n;
  }    
  </code></pre>
  
  <p><strong>AJAX.php</strong></p>
  
  <pre><code>&lt;?php
      require_once("db.php");
          session_start();
          if(!isset($_SESSION["id"])){
      echo "Please login";
      die();
          }
          $id=$_SESSION["id"];
      echo "LOGIN ID = ".$id."&lt;p&gt;&lt;br&gt;&lt;/p&gt;";
      $result=mysql_query("select * from imagedata");
      while($row=mysql_fetch_assoc($result)){
      $image_id=$row["id"];
      $pic=$row["image"];
      $upload_by=$row["upload_by"];
      $no=getCheck("likedata",$id,$image_id);
      $num=getLike("likedata",$image_id);
      if($no&gt;0){
          echo "&lt;p&gt;&lt;img src='$pic' /&gt;&lt;/p&gt;";
          echo "&lt;span id='no_likes'&gt;$num&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;a onclick='ajaxLike($id,$image_id)' id='status'&gt;unlike&lt;/a&gt;";
      }
      else{
          echo "&lt;p&gt;&lt;img src='$pic' /&gt;&lt;/p&gt;";
          echo "&lt;span id='no_likes'&gt;$num&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;a onclick='ajaxLike($id,$image_id)' id='status'&gt;like&lt;/a&gt;";
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p><strong>AJAX.js</strong></p>
  
  <pre><code>  function ajaxLike(id,image_id){
  var doc=new XMLHttpRequest();
  var url="like.php";
  var data="id="+id+"&amp;image_id="+image_id;
  doc.open("POST",url,true);
  doc.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  doc.onreadystatechange=function(){
      if(doc.readyState==4 &amp;&amp; doc.status==200){
          var info=doc.responseText;
          var arr=info.split(" ");
          document.getElementById("no_likes").innerHTML=arr[0];
          document.getElementById("status").innerHTML=arr[1];
      }
  }
  doc.send(data);
  document.getElementById("status").innerHTML="processing.....";
   }
  </code></pre>
  
  <p><strong>Like.php</strong></p>
  
  <pre><code>&lt;?php 
      require_once("db.php");     
      if($_POST["id"]!="" &amp;&amp; $_POST["image_id"]!="" &amp;&amp; $_POST["status"]!=""){
          $id=$_POST["id"];
          $image_id=$_POST["image_id"];
          $status=$_POST["status"];
          $no=mysql_num_rows(mysql_query("select * from likedata where liked_by=$id and image_id=$image_id"));
          if($no&gt;0){
              mysql_query("delete from likedata where liked_by=$id and image_id=$image_id");
              $num=mysql_num_rows(mysql_query("select liked_by from likedata where image_id=$image_id"));
              echo $num." "."like";
  
          }
          else{
              mysql_query("insert into likedata values($id,$image_id)");
              $num=mysql_num_rows(mysql_query("select liked_by from likedata where image_id=$image_id"));
              echo $num." "."unlike";
          }
  
      }
      ?&gt;    
  </code></pre>
  
  <p>When i check the database after click on like/unlike button it show me the result which i want. Bt it doesn't show on page without refreshing page by F5. Help..</p>