• We just launched and are currently in beta. Join us as we build and grow the community.

Easy and Simple Like/Unlike using AJAX/jQuery

bll97569

Email Campaign Revenue Specialist
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
116
Likes
159
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
In this tutorial, I'm going to show you how to create a simple like/unlike using AJAX/JQuery. The purpose of using ajax/jquery is that you won't need to reload the page after liking/unliking. This tutorial won't give you a good design but will give you idea on the topic.

Creating our Database

First, we're going to create our database. This will store our sample data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "like".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE

    TABLE

    `like`

    (
  2. `likeid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `postid`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  4. `userid`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  5. PRIMARY

    KEY

    (

    `likeid`

    )
  6. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

  1. CREATE

    TABLE

    `post`

    (
  2. `postid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `post_text`

    VARCHAR

    (

    200

    )

    NOT

    NULL

    ,
  4. `post_date`

    datetime NOT

    NULL

    ,
  5. PRIMARY

    KEY

    (

    `postid`

    )
  6. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

  1. CREATE

    TABLE

    `user`

    (
  2. `userid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `username`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  4. `password`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  5. `your_name`

    VARCHAR

    (

    60

    )

    NOT

    NULL

    ,
  6. PRIMARY

    KEY

    (

    `userid`

    )
  7. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_4.png

Inserting our Sample Data

Next Step is to insert our sample data into our database.
1.Click "like" database that we have created earlier.
2.Click SQL and paste the below code.

  1. INSERT

    INTO

    `post`

    (

    `post_text`

    ,

    `post_date`

    )

    VALUES
  2. (

    'I got it'

    ,

    '2017-08-02 08:00:00'

    )

    ,
  3. (

    'Eureka'

    ,

    '2017-09-23 08:00:00'

    )

    ,
  4. (

    'Awesome'

    ,

    '2017-09-04 08:00:00'

    )

    ,
  5. (

    'Wow'

    ,

    '2017-09-21 00:00:00'

    )

    ;

  1. INSERT

    INTO

    `user`

    (

    `username`

    ,

    `password`

    ,

    `your_name`

    )

    VALUES
  2. (

    'neovic'

    ,

    'devierte'

    ,

    'neovic devierte'

    )

    ,
  3. (

    'lee'

    ,

    'ann'

    ,

    'lee ann'

    )

    ;

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

  1. <?php

  2. //MySQLi Procedural
  3. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "like"

    )

    ;
  4. if

    (

    !

    $conn

    )

    {
  5. die

    (

    "Connection failed: "

    .

    mysqli_connect_error

    (

    )

    )

    ;
  6. }

  7. ?>

Creating our Login Page

Next, we create our login page to determine our user that we are going to use in liking/unliking. Use the sample users that we have inserted in logging into the system. We name this as "index.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>FB Like using PHP/MySQLi, Ajax/JQuery</title>
  5. </head>
  6. <body>
  7. <h2>Login Here</h2>
  8. <form method="POST" action="login.php">
  9. Username: <input type="text" name="username">
  10. Password: <input type="password" name="password"> <br><br>
  11. <input type="submit" value="Login">
  12. </form><br>
  13. <?php
  14. session_start

    (

    )

    ;
  15. if

    (

    isset

    (

    $_SESSION

    [

    'message'

    ]

    )

    )

    {
  16. echo

    $_SESSION

    [

    'message'

    ]

    ;
  17. unset

    (

    $_SESSION

    [

    'message'

    ]

    )

    ;
  18. }
  19. ?>
  20. </body>
  21. </html>

Creating our Login Code

Next step is to create our login code that will check the user login input. We name this as "login.php".

  1. <?php
  2. session_start

    (

    )

    ;
  3. include

    (

    'conn.php'

    )

    ;

  4. $username

    =

    $_POST

    [

    'username'

    ]

    ;
  5. $password

    =

    $_POST

    [

    'password'

    ]

    ;

  6. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user` where username='$username

    ' and password='$password

    '"

    )

    ;

  7. if

    (

    mysqli_num_rows

    (

    $query

    )

    <

    1

    )

    {
  8. $_SESSION

    [

    'message'

    ]

    =

    "Login Error. Please Try Again"

    ;
  9. header

    (

    'location:index.php'

    )

    ;
  10. }
  11. else

    {
  12. $row

    =

    mysqli_fetch_array

    (

    $query

    )

    ;
  13. $_SESSION

    [

    'userid'

    ]

    =

    $row

    [

    'userid'

    ]

    ;
  14. header

    (

    'location:home.php'

    )

    ;
  15. }

  16. ?>

Creating our Login Success Page

Next, we create our goto page if login is successful. This will be the page that our sample post will be posted for us to like. We name this as "home.php".

  1. <?php
  2. include

    (

    'conn.php'

    )

    ;
  3. session_start

    (

    )

    ;
  4. if

    (

    !

    isset

    (

    $_SESSION

    [

    'userid'

    ]

    )

    ||

    (

    trim

    (

    $_SESSION

    [

    'userid'

    ]

    )

    ==

    ''

    )

    )

    {
  5. header

    (

    'location:index.php'

    )

    ;
  6. exit

    (

    )

    ;
  7. }

  8. $uquery

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user` where userid='"

    .

    $_SESSION

    [

    'userid'

    ]

    .

    "'"

    )

    ;
  9. $urow

    =

    mysqli_fetch_assoc

    (

    $uquery

    )

    ;
  10. ?>
  11. <!DOCTYPE html>
  12. <html>
  13. <head>
  14. <title>Simple FB Like using PHP/MySQLi, Ajax/JQuery</title>
  15. </head>
  16. <body>
  17. <div>
  18. <h2>Welcome, <?php

    echo

    $urow

    [

    'your_name'

    ]

    ;

    ?>

    <a href="logout.php">Logout</a></h2>
  19. <h2>Timeline</h2>
  20. <?php
  21. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `post`"

    )

    ;
  22. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  23. ?>
  24. <div>
  25. Date: <?php

    echo

    date

    (

    'M-d-Y h:i A'

    ,

    strtotime

    (

    $row

    [

    'post_date'

    ]

    )

    )

    ;

    ?>

    <br>
  26. Post: <?php

    echo

    $row

    [

    'post_text'

    ]

    ;

    ?>

    <br>
  27. <div>

  28. <?php
  29. $query1

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `like` where postid='"

    .

    $row

    [

    'postid'

    ]

    .

    "' and userid='"

    .

    $_SESSION

    [

    'userid'

    ]

    .

    "'"

    )

    ;
  30. if

    (

    mysqli_num_rows

    (

    $query1

    )

    >

    0

    )

    {
  31. ?>
  32. <button value="<?php

    echo

    $row

    [

    'postid'

    ]

    ;

    ?>

    " class="unlike">Unlike</button>
  33. <?php
  34. }
  35. else

    {
  36. ?>
  37. <button value="<?php

    echo

    $row

    [

    'postid'

    ]

    ;

    ?>

    " class="like">Like</button>
  38. <?php
  39. }
  40. ?>
  41. <span id="show_like<?php

    echo

    $row

    [

    'postid'

    ]

    ;

    ?>

    ">
  42. <?php
  43. $query3

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `like` where postid='"

    .

    $row

    [

    'postid'

    ]

    .

    "'"

    )

    ;
  44. echo

    mysqli_num_rows

    (

    $query3

    )

    ;
  45. ?>
  46. </span>
  47. </div>
  48. </div><br>
  49. <?php
  50. }
  51. ?>
  52. </div>

  53. <script src = "jquery-3.1.1.js"></script>
  54. <script type = "text/javascript">
  55. $(document).ready(function(){

  56. $(document).on('click', '.like', function(){
  57. var id=$(this).val();
  58. var $this = $(this);
  59. $this.toggleClass('like');
  60. if($this.hasClass('like')){
  61. $this.text('Like');
  62. } else {
  63. $this.text('Unlike');
  64. $this.addClass("unlike");
  65. }
  66. $.ajax({
  67. type: "POST",
  68. url: "like.php",
  69. data: {
  70. id: id,
  71. like: 1,
  72. },
  73. success: function(){
  74. showLike(id);
  75. }
  76. });
  77. });

  78. $(document).on('click', '.unlike', function(){
  79. var id=$(this).val();
  80. var $this = $(this);
  81. $this.toggleClass('unlike');
  82. if($this.hasClass('unlike')){
  83. $this.text('Unlike');
  84. } else {
  85. $this.text('Like');
  86. $this.addClass("like");
  87. }
  88. $.ajax({
  89. type: "POST",
  90. url: "like.php",
  91. data: {
  92. id: id,
  93. like: 1,
  94. },
  95. success: function(){
  96. showLike(id);
  97. }
  98. });
  99. });

  100. });

  101. function showLike(id){
  102. $.ajax({
  103. url: 'show_like.php',
  104. type: 'POST',
  105. async: false,
  106. data:{
  107. id: id,
  108. showlike: 1
  109. },
  110. success: function(response){
  111. $('#show_like'+id).html(response);

  112. }
  113. });
  114. }

  115. </script>
  116. </body>
  117. </html>

Creating our Like Code

Next, we create our like code. This will be the code that will process the uses click on our like button. We name this as "like.php".

  1. <?php
  2. include

    (

    'conn.php'

    )

    ;
  3. session_start

    (

    )

    ;

  4. if

    (

    isset

    (

    $_POST

    [

    'like'

    ]

    )

    )

    {

  5. $id

    =

    $_POST

    [

    'id'

    ]

    ;
  6. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `like` where postid='$id

    ' and userid='"

    .

    $_SESSION

    [

    'userid'

    ]

    .

    "'"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;

  7. if

    (

    mysqli_num_rows

    (

    $query

    )

    >

    0

    )

    {
  8. mysqli_query

    (

    $conn

    ,

    "delete from `like` where userid='"

    .

    $_SESSION

    [

    'userid'

    ]

    .

    "' and postid='$id

    '"

    )

    ;
  9. }
  10. else

    {
  11. mysqli_query

    (

    $conn

    ,

    "insert into `like` (userid,postid) values ('"

    .

    $_SESSION

    [

    'userid'

    ]

    .

    "', '$id

    ')"

    )

    ;
  12. }
  13. }
  14. ?>

Creating our Show Like Code

Next step is to create our show-like code. This code will show the number of likes in the post. We name this as "show_like.php".

  1. <?php
  2. session_start

    (

    )

    ;
  3. include

    (

    'conn.php'

    )

    ;

  4. if

    (

    isset

    (

    $_POST

    [

    'showlike'

    ]

    )

    )

    {
  5. $id

    =

    $_POST

    [

    'id'

    ]

    ;
  6. $query2

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `like` where postid='$id

    '"

    )

    ;
  7. echo

    mysqli_num_rows

    (

    $query2

    )

    ;
  8. }
  9. ?>

Creating our Logout Code

Lastly, we create our logout code. We name this as "logout.php".

  1. <?php
  2. session_start

    (

    )

    ;

  3. session_destroy

    (

    )

    ;
  4. header

    (

    'location:index.php'

    )

    ;

  5. ?>


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top