我想使用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");
}
}
});
}
},
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();
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);
}
我希望它在点击删除事件。然后当它被拖动和下垂时,它应该在数据库上更新。
当您使用fullcalendar V4时,您不能使用$ .fullCalendar,因为V4是纯javascript(即普通js / vanilla js),因为评论V4中提到的@ADYson不再使用jQuery。
其次V4的FullCalendar.formatDate(日期,设置)完全不同的实现,你可以检查所有选项here。
因此,您可以使用moment format()函数,如下所示,
e.g
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>