Dokubo Whyte
Dokubo Whyte

Reputation: 11

Editing and deleting events in fullcalendar v4

I want to use ajax to edit events and delete events using full calendar but for some reason it doesn't work. I think it's a slight issue with my code and I would like some help in fixing it.

This is for PHP using fullcalendar v4, mySQL, AJAX, Javascript

   eventDrop: function (info) {
                var start = moment(info.event.start).format("Y-MM-DD HH:mm:ss");
                var end = moment(info.event.end).format("Y-MM-DD HH:mm:ss");
                var title = info.event.title;
                $.ajax({
                    url: 'edit-event.php',
                    dataType: 'json',
                    data: { start: start, end: end, title : title },
                    type: "POST",
                    success: function (response) {
                        calendar.fullCalendar( info.event.title);
                        //Above is the proper way to reference what I am posting with AJAX
                        displayMessage("Updated Successfully");
                    }
                });
            },

            eventClick: function (info) {
                var deleteMsg = confirm("Do you really want to delete?");
                if (deleteMsg) {
                    $.ajax({
                        type: "POST",
                        url: 'delete-event.php',

                        data: { title : title },
                        success: function (response) {
                            if (parseInt(response) > 0) {
                                calendar.fullCalendar('removeEvents', info.event.title);//call the 'removeEvents' built in function and reference "var title"
                                displayMessage("Deleted Successfully");
                            }
                        }
                    });
                }
            },

edit-event.php

include "dbconnect.php";

if(isset($_POST['title'])){

$title = $_POST['title'];
$start = $_POST['start'];
$end = $_POST['end'];

  $sqlUpdate = "UPDATE events_table SET start='" . $start . "',end='" . $end . "' WHERE title= '$title' ";

  $conn->query($sqlUpdate);
 }
 $conn->close();

delete-event.php

 include "dbconnect.php";

 if(isset($_POST['title'])){
 $title = $_POST['title'];

 echo $sqlDelete = "DELETE from events_table WHERE title='$title'";


 mysqli_query($conn, $sqlDelete);
 echo mysqli_affected_rows($conn);
 }

I expect it to on click remove the event .Then when it is dragged and droped it should update on the database.

Upvotes: 1

Views: 4321

Answers (1)

Shoyeb Sheikh
Shoyeb Sheikh

Reputation: 2866

As you are using fullcalendar V4, you cannot use $.fullCalendar because V4 is pure javascript (i.e plain js/vanilla js) as @ADYson mentioned in the comment V4 doesn't use jQuery anymore.

Secondly V4's FullCalendar.formatDate( date, settings ) have different implementation altogether, you can check all the options here.

For that reason you can use moments format() function as follows,

e.g

var start = moment(info.event.start).format("Y-MM-DD HH:mm:ss");

Here you get desired format in start i.e "Y-MM-DD HH:mm:ss", make sure you have moment js.

Full code,

<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8' />
    <link href='../packages/core/main.css' rel='stylesheet' />
    <link href='../packages/daygrid/main.css' rel='stylesheet' />
    <script src='../packages/core/main.js'></script>
    <script src='../packages/interaction/main.js'></script>
    <script src='../packages/daygrid/main.js'></script>
    <script src="https://momentjs.com/downloads/moment.js"></script>
    <script>

        document.addEventListener('DOMContentLoaded', function () {
            var calendarEl = document.getElementById('calendar');

            var calendar = new FullCalendar.Calendar(calendarEl, {
                plugins: ['interaction', 'dayGrid'],
                defaultDate: '2019-04-12',
                editable: true,
                eventLimit: true, // allow "more" link when too many events
                events: [
                    {
                        title: 'All Day Event',
                        start: '2019-04-01'
                    },
                    {
                        title: 'Long Event',
                        start: '2019-04-07',
                        end: '2019-04-10'
                    },
                    {
                        groupId: 999,
                        title: 'Repeating Event',
                        start: '2019-04-09T16:00:00'
                    },
                    {
                        groupId: 999,
                        title: 'Repeating Event',
                        start: '2019-04-16T16:00:00'
                    },
                    {
                        title: 'Conference',
                        start: '2019-04-11',
                        end: '2019-04-13'
                    },
                    {
                        title: 'Meeting',
                        start: '2019-04-12T10:30:00',
                        end: '2019-04-12T12:30:00'
                    },
                    {
                        title: 'Lunch',
                        start: '2019-04-12T12:00:00'
                    },
                    {
                        title: 'Meeting',
                        start: '2019-04-12T14:30:00'
                    },
                    {
                        title: 'Happy Hour',
                        start: '2019-04-12T17:30:00'
                    },
                    {
                        title: 'Dinner',
                        start: '2019-04-12T20:00:00'
                    },
                    {
                        title: 'Birthday Party',
                        start: '2019-04-13T07:00:00'
                    },
                    {
                        title: 'Click for Google',
                        url: 'http://google.com/',
                        start: '2019-04-28'
                    }
                ],
                eventDrop: function (info) {
                    var start = moment(info.event.start).format("Y-MM-DD HH:mm:ss");
                    var end = moment(info.event.start).format("Y-MM-DD HH:mm:ss");
                    $.ajax({
                        url: 'edit-event.php',
                        data: { start: start, end: end, id: id },
                        type: "POST",
                        success: function (response) {
                            displayMessage("Updated Successfully");
                        }
                    });
                },

                eventClick: function (info) {
                    var deleteMsg = confirm("Do you really want to delete?");
                    if (deleteMsg) {
                        $.ajax({
                            type: "POST",
                            url: 'delete-event.php',
                            data: { id: id },
                            success: function (response) {
                                if (parseInt(response) > 0) {
                                    info.event.remove(); 
                                    displayMessage("Deleted Successfully");
                                }
                            }
                        });
                    }
                }
            });

            calendar.render();
        });

    </script>
    <style>
        body {
            margin: 40px 10px;
            padding: 0;
            font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
            font-size: 14px;
        }

        #calendar {
            max-width: 900px;
            margin: 0 auto;
        }
    </style>
</head>

<body>

    <div id='calendar'></div>

</body>

</html>

Upvotes: 2

Related Questions