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

PHP Facebook-Like Notification using AJAX Bootstrap

Slipable

Plugin Creator
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
92
Likes
114
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, I'm going to show you how to create a simple fb like notification using PHP, AJAX and Bootstrap. In this tutorial, a notification will add every time we add a new user.

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 "notif".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE

    TABLE

    `user`

    (
  2. `userid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `firstname`

    VARCHAR

    (

    10

    )

    NOT

    NULL

    ,
  4. `lastname`

    VARCHAR

    (

    10

    )

    NOT

    NULL

    ,
  5. `seen_status`

    INT

    (

    1

    )

    NOT

    NULL

    ,
  6. PRIMARY

    KEY

    (

    `userid`

    )
  7. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_6.png

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"

    ,

    ""

    ,

    "notif"

    )

    ;
  4. if

    (

    !

    $conn

    )

    {
  5. die

    (

    "Connection failed: "

    .

    mysqli_connect_error

    (

    )

    )

    ;
  6. }

  7. ?>

Creating our Main Page

Next step is to create our main page which contains our add form and the place of our notification. Also included in this page is our ajax codes. We name this as "index.php".

  1. <?

    php
  2. include(

    'conn.php'

    )

    ;
  3. ?>
  4. <!

    DOCTYPE html>
  5. <

    html lang =

    "en"

    >
  6. <

    head>
  7. <

    script src=

    "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"

    ></

    script>
  8. <

    link rel=

    "stylesheet"

    href=

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"

    />
  9. <

    script src=

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"

    ></

    script>
  10. <

    title>

    PHP Fb-

    like Notification using AJAX Bootstrap</

    title>
  11. </

    head>
  12. <

    body>
  13. <

    nav class

    =

    "navbar navbar-default"

    >
  14. <

    div class

    =

    "container-fluid"

    >
  15. <

    div class

    =

    "navbar-header"

    >
  16. <

    a class

    =

    "navbar-brand"

    href=

    "#"

    >

    nurhodelta_17</

    a>
  17. </

    div>
  18. <

    ul class

    =

    "nav navbar-nav navbar-right"

    >
  19. <

    li class

    =

    "dropdown"

    >
  20. <

    a href=

    "#"

    class

    =

    "dropdown-toggle"

    data-

    toggle=

    "dropdown"

    ><

    span class

    =

    "label label-pill label-danger count"

    style=

    "border-radius:10px;"

    ></

    span>

    <

    span class

    =

    "glyphicon glyphicon-globe"

    style=

    "font-size:18px;"

    ></

    span></

    a>
  21. <

    ul class

    =

    "dropdown-menu"

    ></

    ul>
  22. </

    li>
  23. </

    ul>
  24. </

    div>
  25. </

    nav>
  26. <

    div style=

    "height:10px;"

    ></

    div>
  27. <

    div class

    =

    "row"

    >
  28. <

    div class

    =

    "col-md-3"

    >
  29. </

    div>
  30. <

    div class

    =

    "col-md-6 well"

    >
  31. <

    div class

    =

    "row"

    >
  32. <

    div class

    =

    "col-lg-12"

    >
  33. <

    center><

    h2 class

    =

    "text-primary"

    >

    PHP Fb-

    like Notification using AJAX Bootstrap</

    h2></

    center>
  34. <

    hr>
  35. <

    div>
  36. <

    form class

    =

    "form-inline"

    method=

    "POST"

    id=

    "add_form"

    >
  37. <

    div class

    =

    "form-group"

    >
  38. <

    label>

    Firstname:</

    label>
  39. <

    input type=

    "text"

    name=

    "firstname"

    id=

    "firstname"

    class

    =

    "form-control"

    >
  40. </

    div>
  41. <

    div class

    =

    "form-group"

    >
  42. <

    label>

    Lastname:</

    label>
  43. <

    input type=

    "text"

    name=

    "lastname"

    id=

    "lastname"

    class

    =

    "form-control"

    >
  44. </

    div>
  45. <

    div class

    =

    "form-group"

    >
  46. <

    input type=

    "submit"

    name=

    "addnew"

    id=

    "addnew"

    class

    =

    "btn btn-primary"

    value=

    "Add"

    >
  47. </

    div>
  48. </

    form>
  49. </

    div>
  50. </

    div>
  51. </

    div><

    br>
  52. <

    div class

    =

    "row"

    >
  53. <

    div id=

    "userTable"

    ></

    div>
  54. </

    div>
  55. </

    div>
  56. <

    div class

    =

    "col-md-3"

    >
  57. </

    div>
  58. </

    div>
  59. </

    body>
  60. <

    script type =

    "text/javascript"

    >
  61. $(

    document)

    .ready

    (

    function

    (

    )

    {

  62. function

    load_unseen_notification(

    view =

    ''

    )
  63. {
  64. $.ajax

    (

    {
  65. url:

    "fetch.php"

    ,
  66. method:

    "POST"

    ,
  67. data:

    {

    view:

    view}

    ,
  68. dataType:

    "json"

    ,
  69. success:

    function

    (

    data)
  70. {
  71. $(

    '.dropdown-menu'

    )

    .html

    (

    data.notification

    )

    ;
  72. if

    (

    data.unseen_notification

    >

    0

    )

    {
  73. $(

    '.count'

    )

    .html

    (

    data.unseen_notification

    )

    ;
  74. }
  75. }
  76. }

    )

    ;
  77. }

  78. load_unseen_notification(

    )

    ;

  79. $(

    '#add_form'

    )

    .on

    (

    'submit'

    ,

    function

    (

    event)

    {
  80. event.preventDefault

    (

    )

    ;
  81. if

    (

    $(

    '#firstname'

    )

    .val

    (

    )

    !=

    ''

    &&

    $(

    '#lastname'

    )

    .val

    (

    )

    !=

    ''

    )

    {
  82. var

    form_data =

    $(

    this

    )

    .serialize

    (

    )

    ;
  83. $.ajax

    (

    {
  84. url:

    "addnew.php"

    ,
  85. method:

    "POST"

    ,
  86. data:

    form_data,
  87. success:

    function

    (

    data)
  88. {
  89. $(

    '#add_form'

    )

    [

    0

    ]

    .reset

    (

    )

    ;
  90. load_unseen_notification(

    )

    ;
  91. }
  92. }

    )

    ;
  93. }
  94. else

    {
  95. alert(

    "Enter Data First"

    )

    ;
  96. }
  97. }

    )

    ;

  98. $(

    document)

    .on

    (

    'click'

    ,

    '.dropdown-toggle'

    ,

    function

    (

    )

    {
  99. $(

    '.count'

    )

    .html

    (

    ''

    )

    ;
  100. load_unseen_notification(

    'yes'

    )

    ;
  101. }

    )

    ;

  102. setInterval(

    function

    (

    )

    {
  103. load_unseen_notification(

    )

    ;;
  104. }

    ,

    5000

    )

    ;

  105. }

    )

    ;
  106. </

    script>
  107. </

    html>

Creating our Fetch Code

Next is to create our code in fetching data from database that whenever a row is added, it will return to us via notification. We name this as "fetch.php".

  1. <?php
  2. //fetch.php;
  3. if

    (

    isset

    (

    $_POST

    [

    "view"

    ]

    )

    )

    {

  4. include

    (

    "conn.php"

    )

    ;
  5. if

    (

    $_POST

    [

    "view"

    ]

    !=

    ''

    )

    {
  6. mysqli_query

    (

    $conn

    ,

    "update `user` set seen_status='1' where seen_status='0'"

    )

    ;
  7. }

  8. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user` order by userid desc limit 10"

    )

    ;
  9. $output

    =

    ''

    ;

  10. if

    (

    mysqli_num_rows

    (

    $query

    )

    >

    0

    )

    {
  11. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  12. $output

    .=

    '
  13. <li>
  14. <a href="#">
  15. Firstname: <strong>'

    .

    $row

    [

    'firstname'

    ]

    .

    '</strong><br />
  16. Lastname: <strong>'

    .

    $row

    [

    'lastname'

    ]

    .

    '</strong>
  17. </a>
  18. </li>

  19. '

    ;
  20. }
  21. }
  22. else

    {
  23. $output

    .=

    '<li><a href="#" class="text-bold text-italic">No Notification Found</a></li>'

    ;
  24. }

  25. $query1

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user` where seen_status='0'"

    )

    ;
  26. $count

    =

    mysqli_num_rows

    (

    $query1

    )

    ;
  27. $data

    =

    array

    (
  28. 'notification'

    =>

    $output

    ,
  29. 'unseen_notification'

    =>

    $count
  30. )

    ;
  31. echo

    json_encode

    (

    $data

    )

    ;

  32. }
  33. ?>

Creating our Add Code

Last is our add code that adds rows into our database. We name this as "addnew.php".

  1. <?php
  2. //insert.php
  3. if

    (

    isset

    (

    $_POST

    [

    "firstname"

    ]

    )

    )
  4. {
  5. include

    (

    'conn.php'

    )

    ;
  6. $firstname

    =

    mysqli_real_escape_string

    (

    $conn

    ,

    $_POST

    [

    'firstname'

    ]

    )

    ;
  7. $lastname

    =

    mysqli_real_escape_string

    (

    $conn

    ,

    $_POST

    [

    'lastname'

    ]

    )

    ;

  8. mysqli_query

    (

    $conn

    ,

    "insert into `user` (firstname, lastname) values ('$firstname

    ', '$lastname

    ')"

    )

    ;
  9. }
  10. ?>


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

452,292

323,526

323,535

Top