在Fullcalendar v4中编辑和删除事件

时间:2019-04-13 14:51:51

标签: javascript php ajax fullcalendar

我想使用ajax编辑事件并使用完整日历删除事件,但由于某些原因,它不起作用。我认为这是我的代码中的一个小问题,我希望在修复它方面有所帮助。

这是针对使用fullcalendar v4,mySQL,AJAX,Javascript的PHP

   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);
 }

我希望它点击删除事件 然后,将其拖放到数据库上时应进行更新。

1 个答案:

答案 0 :(得分:2)

由于您使用的是fullcalendar V4,因此无法使用$ .fullCalendar,因为V4是纯JavaScript(例如,纯js / vanilla js),因为注释A4中提到的@ADYson不再使用jQuery。

第二个V4的FullCalendar.formatDate(date,settings)完全具有不同的实现,您可以检查所有选项here

因此,您可以按以下方式使用矩format()函数,

例如

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

在这里您可以从开始获得所需的格式,即“ Y-MM-DD HH:mm:ss”,请确保您有一个js时刻。

完整代码

<!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>