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

Event CRUD in FullCalendar using PHP and jQuery Tutorial

chile666

Loot Collector
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
122
Likes
27
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, I will show how to Create CRUD Operation in FullCalendar with PHP and MySQL Database. here, you will learn to add, edit, view, and delete events on the calendar. The events will be stored in MySQL Database. This feature can be useful for your future PHP Projects such as Appointment Management Systems or Scheduling Systems.

We will create a simple PHP application with dummy MySQL data that stores the events/schedules. The main goal of the application is to have features or functionalities that adds, updates, view, and delete events in FullCalendar.

Getting Started

Download XAMPP as your local web server to run our PHP Script. After Installing the virtual server, open the XAMPP's Control Panel and start the Apache Server and MySQL.

Since, that events will be shown in a calendar view using the FullCalendar library/plugin, download the library's resources here.

Download Bootstrap v5 and jQuery for the interface design of the application that we'll be creating. After that move the library directory to the folder where you will store the source code on your end.

Creating The Database

Open a new tab in your browser and browse the XAMPP's PHPMyAdmin

. Next, create a new database naming dummy_db. Then, navigate the page into the SQL Tab/Page and paste SQL Script below to the provided text field. Lastly, click the Go Button to execute the script.

  1. CREATE

    TABLE

    `schedule_

    list`

    (
  2. `id`

    int

    (

    30

    )

    NOT

    NULL

    PRIMARY KEY

    AUTO_INCREMENT

    ,
  3. `title`

    text

    NOT

    NULL

    ,
  4. `description`

    text

    NOT

    NULL

    ,
  5. `start_

    datetime`

    datetime

    NOT

    NULL

    ,
  6. `end_

    datetime`

    datetime

    DEFAULT

    NULL


  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    utf8mb4;

Creating The Database Connection

Open a text editor such as sublime text, notepad++, or VSCode. Create a new PHP File naming db-connect.php. Then, Copy/Paste the code below. Make sure to configure the database credentials according to your setup.

  1. <?php
  2. $host

    =

    'localhost'

    ;
  3. $username

    =

    'root'

    ;
  4. $password

    =

    ''

    ;
  5. $dbname

    =

    'dummy_db'

    ;

  6. $conn

    =

    new

    mysqli(

    $host

    ,

    $username

    ,

    $password

    ,

    $dbname

    )

    ;
  7. if

    (

    !

    $conn

    )

    {
  8. die

    (

    "Cannot connect to the database."

    .

    $conn

    ->

    error

    )

    ;
  9. }

Or, you can also import the SQL File I provided along with working source code file. The file is known as dummy_db.sql and located inside the db folder.

Creating The Interface

Next, we will be creating the interface of our page. The page contains the HTML elements and PHP script that shows the data and query data in the database. Copy/Paste the script below and save it as index.php.

  1. <?php require_once(

    'db-connect.php'

    )

    ?>
  2. <!DOCTYPE html>
  3. <html

    lang

    =

    "en"

    >

  4. <head

    >
  5. <meta

    charset

    =

    "UTF-8"

    >
  6. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  7. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  8. <title

    >

    Scheduling</

    title

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://pro.fontawesome.com/releases/v5.10.0/css/all.css"

    integrity=

    "sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p"

    crossorigin=

    "anonymous"

    /

    >
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "./css/bootstrap.min.css"

    >
  11. <link

    rel

    =

    "stylesheet"

    href

    =

    "./fullcalendar/lib/main.min.css"

    >
  12. <script

    src

    =

    "./js/jquery-3.6.0.min.js"

    ></

    script

    >
  13. <script

    src

    =

    "./js/bootstrap.min.js"

    ></

    script

    >
  14. <script

    src

    =

    "./fullcalendar/lib/main.min.js"

    ></

    script

    >
  15. <style

    >
  16. :root {
  17. --bs-success-rgb: 71, 222, 152 !important;
  18. }

  19. html,
  20. body {
  21. height: 100%;
  22. width: 100%;
  23. font-family: Apple Chancery, cursive;
  24. }

  25. .btn-info.text-light:hover,
  26. .btn-info.text-light:focus {
  27. background: #000;
  28. }
  29. table, tbody, td, tfoot, th, thead, tr {
  30. border-color: #ededed !important;
  31. border-style: solid;
  32. border-width: 1px !important;
  33. }
  34. </

    style

    >
  35. </

    head

    >

  36. <body

    class

    =

    "bg-light"

    >
  37. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-dark bg-gradient"

    id

    =

    "topNavBar"

    >
  38. <div

    class

    =

    "container"

    >
  39. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >
  40. Sourcecodester
  41. </

    a

    >

  42. <div

    >
  43. <b

    class

    =

    "text-light"

    >

    Sample Scheduling</

    b

    >
  44. </

    div

    >
  45. </

    div

    >
  46. </

    nav>
  47. <div

    class

    =

    "container py-5"

    id

    =

    "page-container"

    >
  48. <div

    class

    =

    "row"

    >
  49. <div

    class

    =

    "col-md-9"

    >
  50. <div

    id

    =

    "calendar"

    ></

    div

    >
  51. </

    div

    >
  52. <div

    class

    =

    "col-md-3"

    >
  53. <div

    class

    =

    "cardt rounded-0 shadow"

    >
  54. <div

    class

    =

    "card-header bg-gradient bg-primary text-light"

    >
  55. <h5

    class

    =

    "card-title"

    >

    Schedule Form</

    h5

    >
  56. </

    div

    >
  57. <div

    class

    =

    "card-body"

    >
  58. <div

    class

    =

    "container-fluid"

    >
  59. <form

    action

    =

    "save_schedule.php"

    method

    =

    "post"

    id

    =

    "schedule-form"

    >
  60. <input

    type

    =

    "hidden"

    name

    =

    "id"

    value

    =

    ""

    >
  61. <div

    class

    =

    "form-group mb-2"

    >
  62. <label

    for

    =

    "title"

    class

    =

    "control-label"

    >

    Title</

    label

    >
  63. <input

    type

    =

    "text"

    class

    =

    "form-control form-control-sm rounded-0"

    name

    =

    "title"

    id

    =

    "title"

    required>
  64. </

    div

    >
  65. <div

    class

    =

    "form-group mb-2"

    >
  66. <label

    for

    =

    "description"

    class

    =

    "control-label"

    >

    Description</

    label

    >
  67. <textarea

    rows

    =

    "3"

    class

    =

    "form-control form-control-sm rounded-0"

    name

    =

    "description"

    id

    =

    "description"

    required></

    textarea

    >
  68. </

    div

    >
  69. <div

    class

    =

    "form-group mb-2"

    >
  70. <label

    for

    =

    "start_datetime"

    class

    =

    "control-label"

    >

    Start</

    label

    >
  71. <input

    type

    =

    "datetime-local"

    class

    =

    "form-control form-control-sm rounded-0"

    name

    =

    "start_datetime"

    id

    =

    "start_datetime"

    required>
  72. </

    div

    >
  73. <div

    class

    =

    "form-group mb-2"

    >
  74. <label

    for

    =

    "end_datetime"

    class

    =

    "control-label"

    >

    End</

    label

    >
  75. <input

    type

    =

    "datetime-local"

    class

    =

    "form-control form-control-sm rounded-0"

    name

    =

    "end_datetime"

    id

    =

    "end_datetime"

    required>
  76. </

    div

    >
  77. </

    form

    >
  78. </

    div

    >
  79. </

    div

    >
  80. <div

    class

    =

    "card-footer"

    >
  81. <div

    class

    =

    "text-center"

    >
  82. <button

    class

    =

    "btn btn-primary btn-sm rounded-0"

    type

    =

    "submit"

    form=

    "schedule-form"

    ><i

    class

    =

    "fa fa-save"

    ></

    i

    >

    Save</

    button

    >
  83. <button

    class

    =

    "btn btn-default border btn-sm rounded-0"

    type

    =

    "reset"

    form=

    "schedule-form"

    ><i

    class

    =

    "fa fa-reset"

    ></

    i

    >

    Cancel</

    button

    >
  84. </

    div

    >
  85. </

    div

    >
  86. </

    div

    >
  87. </

    div

    >
  88. </

    div

    >
  89. </

    div

    >
  90. <!-- Event Details Modal -->
  91. <div

    class

    =

    "modal fade"

    tabindex

    =

    "-1"

    data-bs-backdrop=

    "static"

    id

    =

    "event-details-modal"

    >
  92. <div

    class

    =

    "modal-dialog modal-dialog-centered"

    >
  93. <div

    class

    =

    "modal-content rounded-0"

    >
  94. <div

    class

    =

    "modal-header rounded-0"

    >
  95. <h5

    class

    =

    "modal-title"

    >

    Schedule Details</

    h5

    >
  96. <button

    type

    =

    "button"

    class

    =

    "btn-close"

    data-bs-dismiss=

    "modal"

    aria-label

    =

    "Close"

    ></

    button

    >
  97. </

    div

    >
  98. <div

    class

    =

    "modal-body rounded-0"

    >
  99. <div

    class

    =

    "container-fluid"

    >
  100. <dl

    >
  101. <dt

    class

    =

    "text-muted"

    >

    Title</

    dt

    >
  102. <dd

    id

    =

    "title"

    class

    =

    "fw-bold fs-4"

    ></

    dd

    >
  103. <dt

    class

    =

    "text-muted"

    >

    Description</

    dt

    >
  104. <dd

    id

    =

    "description"

    class

    =

    ""

    ></

    dd

    >
  105. <dt

    class

    =

    "text-muted"

    >

    Start</

    dt

    >
  106. <dd

    id

    =

    "start"

    class

    =

    ""

    ></

    dd

    >
  107. <dt

    class

    =

    "text-muted"

    >

    End</

    dt

    >
  108. <dd

    id

    =

    "end"

    class

    =

    ""

    ></

    dd

    >
  109. </

    dl

    >
  110. </

    div

    >
  111. </

    div

    >
  112. <div

    class

    =

    "modal-footer rounded-0"

    >
  113. <div

    class

    =

    "text-end"

    >
  114. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary btn-sm rounded-0"

    id

    =

    "edit"

    data-id

    =

    ""

    >

    Edit</

    button

    >
  115. <button

    type

    =

    "button"

    class

    =

    "btn btn-danger btn-sm rounded-0"

    id

    =

    "delete"

    data-id

    =

    ""

    >

    Delete</

    button

    >
  116. <button

    type

    =

    "button"

    class

    =

    "btn btn-secondary btn-sm rounded-0"

    data-bs-dismiss=

    "modal"

    >

    Close</

    button

    >
  117. </

    div

    >
  118. </

    div

    >
  119. </

    div

    >
  120. </

    div

    >
  121. </

    div

    >
  122. <!-- Event Details Modal -->

  123. <?php
  124. $schedules =

    $conn->

    query("SELECT * FROM `schedule_list`");
  125. $sched_res = [];
  126. foreach($schedules->fetch_all(MYSQLI_ASSOC) as $row){
  127. $row['sdate'] = date("F d, Y h:i A",strtotime($row['start_datetime']));
  128. $row['edate'] = date("F d, Y h:i A",strtotime($row['end_datetime']));
  129. $sched_res[$row['id']] = $row;
  130. }
  131. ?>
  132. <?php
  133. if(

    isset(

    $conn)

    )

    $conn->

    close();
  134. ?>
  135. </

    body

    >
  136. <script

    >
  137. var scheds = $.parseJSON('<?=

    json_encode(

    $sched_res)

    ?>

    ')
  138. </

    script

    >
  139. <script

    src

    =

    "./js/script.js"

    ></

    script

    >

  140. </

    html

    >

Creating The PHP Queries

Next, we will be creating the PHP queries. The codes below is the scripts that inserts, updates, and delete event data in our database. Save the files according to the filename

above each scripts.

save_schedule.php
This scripts contains the insert and update query scripts.

  1. <?php
  2. require_once

    (

    'db-connect.php'

    )

    ;
  3. if

    (

    $_SERVER

    [

    'REQUEST_METHOD'

    ]

    !=

    'POST'

    )

    {
  4. echo

    "<script> alert('Error: No data to save.'); location.replace('./') </script>"

    ;
  5. $conn

    ->

    close

    (

    )

    ;
  6. exit

    ;
  7. }
  8. extract

    (

    $_POST

    )

    ;
  9. $allday

    =

    isset

    (

    $allday

    )

    ;

  10. if

    (

    empty

    (

    $id

    )

    )

    {
  11. $sql

    =

    "INSERT INTO `schedule_list` (`title`,`description`,`start_datetime`,`end_datetime`) VALUES ('$title

    ','$description

    ','$start_datetime

    ','$end_datetime

    ')"

    ;
  12. }

    else

    {
  13. $sql

    =

    "UPDATE `schedule_list` set `title` = '{$title}

    ', `description` = '{$description}

    ', `start_datetime` = '{$start_datetime}

    ', `end_datetime` = '{$end_datetime}

    ' where `id` = '{$id}

    '"

    ;
  14. }
  15. $save

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  16. if

    (

    $save

    )

    {
  17. echo

    "<script> alert('Schedule Successfully Saved.'); location.replace('./') </script>"

    ;
  18. }

    else

    {
  19. echo

    "<pre>"

    ;
  20. echo

    "An Error occurred.<br>"

    ;
  21. echo

    "Error: "

    .

    $conn

    ->

    error

    .

    "<br>"

    ;
  22. echo

    "SQL: "

    .

    $sql

    .

    "<br>"

    ;
  23. echo

    "</pre>"

    ;
  24. }
  25. $conn

    ->

    close

    (

    )

    ;
  26. ?>

delete_schedule.php
This scripts contains the delete query scripts.

  1. <?php
  2. require_once

    (

    'db-connect.php'

    )

    ;
  3. if

    (

    !

    isset

    (

    $_GET

    [

    'id'

    ]

    )

    )

    {
  4. echo

    "<script> alert('Undefined Schedule ID.'); location.replace('./') </script>"

    ;
  5. $conn

    ->

    close

    (

    )

    ;
  6. exit

    ;
  7. }

  8. $delete

    =

    $conn

    ->

    query

    (

    "DELETE FROM `schedule_list` where id = '{$_GET['id']}

    '"

    )

    ;
  9. if

    (

    $delete

    )

    {
  10. echo

    "<script> alert('Event has deleted successfully.'); location.replace('./') </script>"

    ;
  11. }

    else

    {
  12. echo

    "<pre>"

    ;
  13. echo

    "An Error occured.<br>"

    ;
  14. echo

    "Error: "

    .

    $conn

    ->

    error

    .

    "<br>"

    ;
  15. echo

    "SQL: "

    .

    $sql

    .

    "<br>"

    ;
  16. echo

    "</pre>"

    ;
  17. }
  18. $conn

    ->

    close

    (

    )

    ;

  19. ?>

Creating The Main Function

Lastly, the script below is a javascript codes that initialize the calendar, render the events in the calendar, and some other functionalities. Save this file as script.js.

  1. var

    calendar;
  2. var

    Calendar =

    FullCalendar.Calendar

    ;
  3. var

    events =

    [

    ]

    ;
  4. $(

    function

    (

    )

    {
  5. if

    (

    !!

    scheds)

    {
  6. Object

    .keys

    (

    scheds)

    .map

    (

    k =>

    {
  7. var

    row =

    scheds[

    k]
  8. events.push

    (

    {

    id:

    row.id

    ,

    title:

    row.title

    ,

    start:

    row.start_datetime

    ,

    end:

    row.end_datetime

    }

    )

    ;
  9. }

    )
  10. }
  11. var

    date =

    new

    Date

    (

    )
  12. var

    d =

    date.getDate

    (

    )

    ,
  13. m =

    date.getMonth

    (

    )

    ,
  14. y =

    date.getFullYear

    (

    )

  15. calendar =

    new

    Calendar(

    document.getElementById

    (

    'calendar'

    )

    ,

    {
  16. headerToolbar:

    {
  17. left:

    'prev,next today'

    ,
  18. right:

    'dayGridMonth,dayGridWeek,list'

    ,
  19. center:

    'title'

    ,
  20. }

    ,
  21. selectable:

    true

    ,
  22. themeSystem:

    'bootstrap'

    ,
  23. //Random default events
  24. events:

    events,
  25. eventClick:

    function

    (

    info)

    {
  26. var

    _details =

    $(

    '#event-details-modal'

    )
  27. var

    id =

    info.event

    .id
  28. if

    (

    !!

    scheds[

    id]

    )

    {
  29. _details.find

    (

    '#title'

    )

    .text

    (

    scheds[

    id]

    .title

    )
  30. _details.find

    (

    '#description'

    )

    .text

    (

    scheds[

    id]

    .description

    )
  31. _details.find

    (

    '#start'

    )

    .text

    (

    scheds[

    id]

    .sdate

    )
  32. _details.find

    (

    '#end'

    )

    .text

    (

    scheds[

    id]

    .edate

    )
  33. _details.find

    (

    '#edit,#delete'

    )

    .attr

    (

    'data-id'

    ,

    id)
  34. _details.modal

    (

    'show'

    )
  35. }

    else

    {
  36. alert(

    "Event is undefined"

    )

    ;
  37. }
  38. }

    ,
  39. eventDidMount:

    function

    (

    info)

    {
  40. // Do Something after events mounted
  41. }

    ,
  42. editable:

    true
  43. }

    )

    ;

  44. calendar.render

    (

    )

    ;

  45. // Form reset listener
  46. $(

    '#schedule-form'

    )

    .on

    (

    'reset'

    ,

    function

    (

    )

    {
  47. $(

    this

    )

    .find

    (

    'input:hidden'

    )

    .val

    (

    ''

    )
  48. $(

    this

    )

    .find

    (

    'input:visible'

    )

    .first

    (

    )

    .focus

    (

    )
  49. }

    )

  50. // Edit Button
  51. $(

    '#edit'

    )

    .click

    (

    function

    (

    )

    {
  52. var

    id =

    $(

    this

    )

    .attr

    (

    'data-id'

    )
  53. if

    (

    !!

    scheds[

    id]

    )

    {
  54. var

    _form =

    $(

    '#schedule-form'

    )
  55. console.log

    (

    String

    (

    scheds[

    id]

    .start_datetime

    )

    ,

    String

    (

    scheds[

    id]

    .start_datetime

    )

    .replace

    (

    " "

    ,

    "\\

    t"

    )

    )
  56. _form.find

    (

    '[name="id"]'

    )

    .val

    (

    id)
  57. _form.find

    (

    '[name="title"]'

    )

    .val

    (

    scheds[

    id]

    .title

    )
  58. _form.find

    (

    '[name="description"]'

    )

    .val

    (

    scheds[

    id]

    .description

    )
  59. _form.find

    (

    '[name="start_datetime"]'

    )

    .val

    (

    String

    (

    scheds[

    id]

    .start_datetime

    )

    .replace

    (

    " "

    ,

    "T"

    )

    )
  60. _form.find

    (

    '[name="end_datetime"]'

    )

    .val

    (

    String

    (

    scheds[

    id]

    .end_datetime

    )

    .replace

    (

    " "

    ,

    "T"

    )

    )
  61. $(

    '#event-details-modal'

    )

    .modal

    (

    'hide'

    )
  62. _form.find

    (

    '[name="title"]'

    )

    .focus

    (

    )
  63. }

    else

    {
  64. alert(

    "Event is undefined"

    )

    ;
  65. }
  66. }

    )

  67. // Delete Button / Deleting an Event
  68. $(

    '#delete'

    )

    .click

    (

    function

    (

    )

    {
  69. var

    id =

    $(

    this

    )

    .attr

    (

    'data-id'

    )
  70. if

    (

    !!

    scheds[

    id]

    )

    {
  71. var

    _conf =

    confirm(

    "Are you sure to delete this scheduled event?"

    )

    ;
  72. if

    (

    _conf ===

    true

    )

    {
  73. location.href

    =

    "./delete_schedule.php?id="

    +

    id;
  74. }
  75. }

    else

    {
  76. alert(

    "Event is undefined"

    )

    ;
  77. }
  78. }

    )
  79. }

    )

DEMO VIDEO

That's it! You can now test the application in your browser and see if we have met our goal on this tutorial. If ever you have encountered any errors, please review your source code by differentiating it from the source code I provided above. You can also test the working source code I created for this tutorial. You can download the source code zip file below this article.

That is the end of this tutorial. I hope you'll find this Tutorial useful for your current and future PHP projects.

Happy Coding :)


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

452,496

334,779

334,787

Top