Like and Unlike in PHP JQuery Ajax
Hi Friends today we are going to learn how to create like and unlike system in php using jquery ajax without refresh the page..you can implement like and unlike system in your website for your members..
Live Like and Unlike in PHP JQuery Ajax |
Database Name: test
Table Name: likedislike
Table structure for table `likedislike`
CREATE TABLE IF NOT EXISTS `likedislike` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `uplike` int(20) NOT NULL, `downlike` int(20) NOT NULL, PRIMARY KEY(id) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
-- Dumping data for table `likedislike`
INSERT INTO `likedislike` (`id`, `name`, `uplike`, `downlike`) VALUES (1, 'ANJAN', 0, 0), (2, 'Priya Singh', 0, 0), (3, 'ANJAN KUMAR', 0, 0), (4, 'Manash', 0, 0);
Files Structure:
Create 5 Files..
1. connect.php
2. index.php
3. script.js
4. uplike.php
5.downlike.php
copy the below codes for certain file..
1. connect.php
<?php $conn=new mysqli('localhost','root','','test'); ?>
2. index.php
<html> <head> <title>AJAX PHP and MySQL Live Like Dislike System</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="script.js"></script> </head> <div id="content"> <?php include('connect.php'); $result=mysqli_query($conn,"SELECT*FROM likedislike ORDER BY uplike DESC"); while($row=$result->fetch_array()){?> <strong><?php echo $row['name'];?></strong> <div> <a href="" class="uplike" id="<?php echo $row['id'];?>">Like</a> :<b><font color="green"><?php echo $row['uplike'];?></font></b>|| <a href="" class="downlike" id="<?php echo $row['id'];?>">Dislike</a>:<b><font color="red"><?php echo $row['downlike'];?></font></b> </div> <?php }?> <hr/> </div> </html>
3. script.js
$(document).ready(function(){ //Adding Like $(".uplike").click(function(){ var id=$(this).attr('id'); $.ajax({ url:"uplike.php", type:"POST", data:{id,id}, success: function(data){ $("#content").load("index.php"); } }); return false; }); //Adding Dislike $(".downlike").click(function(){ var id=$(this).attr('id'); $.ajax({ url:"downlike.php", method:"POST", type:{id,id}, success: function(data){ $("#content").load("index.php"); } }); return false; }); });
4. uplike.php
<?php include("connect.php"); if(!empty($_POST['id'])){ $id=$_POST['id']; $result=mysqli_query($conn,"SELECT*FROM likedislike WHERE id='$id'"); $row=$result->fetch_assoc(); $totallike=$row['uplike']+1; //Updating Like ROW From Table $addlike=mysqli_query($conn,"UPDATE likedislike SET uplike='$totallike' WHERE id='$id'"); } ?>
5. downlike.php
<?php include("connect.php"); if(!empty($_POST['id'])){ $id=$_POST['id']; $result=mysqli_query($conn,"SELECT*FROM likedislike WHERE id='$id'"); $row=$result->fetch_assoc(); $totallike=$row['downlike']+1; //Updating Like ROW From Table $addlike=mysqli_query($conn,"UPDATE likedislike SET downlike='$totallike' WHERE id='$id'"); } ?>
You can also implement ip based like and unlike system to your website..
use $_SERVER['REMOTE_ADDR'] to get User Ip Address.. ..if You like this post..please share and don't forget to comment..Thank You..
Also learn...
1. PHP AJAX Live Search
2. Live Username Availability Check in PHP,JQuery AJAX
3. PHP AJAX Insert Data Into Database without page Refresh
4. PHP AJAX Dependent Menu(Country and States)
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.