a digital blog

AJAX, Java Script, JQuery, MySql, PHP

PHP Cookie Ajax Based Like Dislike Script

PHP Cookie Ajax Based Like Dislike Script

This post explains you about how to implement php cookie ajax based like dislike script.

Cookie is a small file that stores on the user’s computer.Cookies is a mechanism for storing data in the remote browser and thus tracking or identifying return users.

In this script I am using cookie for user tracking.

Download Link   Demo Link

 

Sample database design for table name contact.

This table contains id (primary key), name and age.

CREATE TABLE IF NOT EXISTS `product` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(250) NOT NULL,
  `pLike` int(11) NOT NULL,
  `pDislike` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)

dbConnect.inc.php

Contains database connectivity code

$mysql_db_hostname = "Host name";
$mysql_db_user = "UserName";
$mysql_db_password = "Password";
$mysql_db_database = "Database Name";

$con = mysql_connect($mysql_db_hostname, $mysql_db_user,
       $mysql_db_password or die("Could not connect database");
//Create a new connection
mysql_select_db($mysql_db_database, $con) or
                 die("Could not select database");
// select database

index.php

Contains product display code.

include_once('inc/dbConnect.inc.php');
//connect with database
$query="select * from product order by id desc";
// query for fetch all products
$res=mysql_query($query);
$countProduct=mysql_num_rows($res);
// check how many products are avaliable
//Java Script Code 
<script type="text/javascript" src="js/jquery-1.4.1.min.js">
</script>
// jquery library file
<script type="text/javascript">
function changeLikeDislike(type,id){
   var dataString = 'id='+ id + '&type=' + type;
   $("#product_flash_"+id).show();
   $("#product_flash_"+id).fadeIn(400).html
                          ('<img src="image/loading.gif" />');
   $.ajax({
     type: "POST",
     url: "changeLikeDislike.php",
     data: dataString,
     cache: false,
     success: function(result){
     if(result){
        var position=result.indexOf("||");
        var warningMessage=result.substring(0,position);
        if(warningMessage=='success'){
           var successMessage=result.substring(position+2);
           $("#product_flash_"+id).html('&nbsp;');
           $("#product_"+type+"_"+id).html(successMessage);
        }else{
           var errorMessage=result.substring(position+2);
           $("#product_flash_"+id).html(errorMessage);
        }
     }
   }
  });
}
</script>

changeLikeDislike.php

Contains the login part and cookie code.

include_once('inc/dbConnect.inc.php');
 // connect with database
 $type=$_POST['type'];
 $id=$_POST['id'];
 if(isset($_COOKIE['likeDislike'."_".$id])) // check cookie
      echo "error||Already Voted"; // if exist display message
 else{
     if($type=='like'){
         $fieldName='pLike';
     }elseif($type=='dislike'){
         $fieldName='pDislike';
     }else{
         die();
     }
    $query="update product set $fieldName=$fieldName+1 where id='$id'";
     // update like/dislike count
     $res=mysql_query($query);
     $query="select $fieldName from product where id='$id'";
     // check updated new count
     $res=mysql_query($query);
     $result=mysql_fetch_array($res);
     $count=$result[$fieldName];
     $expire=time()+60*60*24*30;
     setcookie("likeDislike"."_".$id, "likeDislike"."_".$id, $expire);
     //set cookie
     echo "success||".$count;
 }

3 Comments

  1. You made certain good points there. I did a search on the issue and found a good number of persons will consent with your blog.

  2. When I first saw this title PHP Cookie Ajax Based Like Dislike Script | 91 Web Lessons on google I just whent and bookmark it. These look great. I always find that illustrations on a website add a much friendlier tone.


*

Copyright © 91 Web Lessons - a digital blog