Show The Event Calendar lists the events date-wise in a large calendar view. The events are fetched dynamically and listed under the date cell. An event calendar makes it easier to find the events for a specific date. You can build a custom event calendar with dynamic data using PHP and MySQL. In the previous tutorial, we learn how to build an event calendar with PHP and MySQL. This tutorial will show you how to create an event calendar with PHP using the FullCalendar JavaScript library. FullCalendar is a JavaScript library that helps to integrate a Full-sized event calendar on a website quickly. It provides a user-friendly way to add the events to the calendar and display them on the date cell. We will use the FullCalendar library to build an event calendar with PHP and MySQL. Not only the event listing but also we will integrate CRUD functionality with FullCalendar. The following functionality will be implemented to build PHP event calendar with FullCalendar.
Before getting started, take a look at the file structure of the PHP event calendar with FullCalendar JS plugin script. php_event_calendar_with_fullcalendar/ ├── index.html ├── fetchEvents.php ├── eventHandler.php ├── dbConfig.php ├── js/ │ ├── fullcalendar/ │ └── sweetalert2.all.min.js └── css/ └── style.css Create Database TableTo display the dynamic events in the calendar, the event data need to be stored in the database. The following SQL creates a CREATE TABLE `events` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `description` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL, `url` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `start` date NOT NULL, `end` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; Database Configuration (dbConfig.php)This file is used to connect with the database using PHP and MySQL. Specify the database host (DB_HOST), username (DB_USERNAME), password (DB_PASSWORD), and name (DB_NAME) as per your database credentials. <?php Build Event Calendar (index.html)Dialog Boxes: Include the sweetalert2 plugin library to initialize and use it for popup boxes. <script src="js/sweetalert2.all.min.js"></script> Calendar Plugin: Include FullCalendar JS & CSS library to build full-sized calendar using JavaScript. <link href="js/fullcalendar/lib/main.css" rel="stylesheet" /> <script src="js/fullcalendar/lib/main.js"></script> Calendar Container: Define an HTML element to attach the event calendar. <div id="calendar"></div> Initialize FullCalendar class on DOMContentLoaded event and render calendar on the web page. <script> document.addEventListener('DOMContentLoaded', function() { var calendarEl = document.getElementById('calendar'); var calendar = new FullCalendar.Calendar(calendarEl, { initialView: 'dayGridMonth', height: 650, }); calendar.render(); }); </script> In the following examples, we will see how to integrate event listing, details, add, and delete functionality to FullCalendar using JavaScript. Fetch events from the server-side and list them on the Calendar:
document.addEventListener('DOMContentLoaded', function() { var calendarEl = document.getElementById('calendar'); var calendar = new FullCalendar.Calendar(calendarEl, { initialView: 'dayGridMonth', height: 650, events: 'fetchEvents.php', }); calendar.render(); }); Add Event:
document.addEventListener('DOMContentLoaded', function() { var calendarEl = document.getElementById('calendar'); var calendar = new FullCalendar.Calendar(calendarEl, { initialView: 'dayGridMonth', height: 650, events: 'fetchEvents.php', selectable: true, select: async function (start, end, allDay) { const { value: formValues } = await Swal.fire({ title: 'Add Event', html: '<input id="swalEvtTitle" class="swal2-input" placeholder="Enter title">' + '<textarea id="swalEvtDesc" class="swal2-input" placeholder="Enter description"></textarea>' + '<input id="swalEvtURL" class="swal2-input" placeholder="Enter URL">', focusConfirm: false, preConfirm: () => { return [ document.getElementById('swalEvtTitle').value, document.getElementById('swalEvtDesc').value, document.getElementById('swalEvtURL').value ] } }); if (formValues) { fetch("eventHandler.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ request_type:'addEvent', start:start.startStr, end:start.endStr, event_data: formValues}), }) .then(response => response.json()) .then(data => { if (data.status == 1) { Swal.fire('Event added successfully!', '', 'success'); } else { Swal.fire(data.error, '', 'error'); } calendar.refetchEvents(); }) .catch(console.error); } } }); calendar.render(); }); View event details:
document.addEventListener('DOMContentLoaded', function() { var calendarEl = document.getElementById('calendar'); var calendar = new FullCalendar.Calendar(calendarEl, { initialView: 'dayGridMonth', height: 650, events: 'fetchEvents.php', eventClick: function(info) { info.jsEvent.preventDefault(); info.el.style.borderColor = 'red'; Swal.fire({ title: info.event.title, icon: 'info', html:'<p>'+info.event.extendedProps.description+'</p><a href="'+info.event.url+'">Visit event page</a>', }); } }); calendar.render(); }); Delete Event:
document.addEventListener('DOMContentLoaded', function() { var calendarEl = document.getElementById('calendar'); var calendar = new FullCalendar.Calendar(calendarEl, { initialView: 'dayGridMonth', height: 650, events: 'fetchEvents.php', selectable: true, select: async function (start, end, allDay) { const { value: formValues } = await Swal.fire({ title: 'Add Event', html: '<input id="swalEvtTitle" class="swal2-input" placeholder="Enter title">' + '<textarea id="swalEvtDesc" class="swal2-input" placeholder="Enter description"></textarea>' + '<input id="swalEvtURL" class="swal2-input" placeholder="Enter URL">', focusConfirm: false, preConfirm: () => { return [ document.getElementById('swalEvtTitle').value, document.getElementById('swalEvtDesc').value, document.getElementById('swalEvtURL').value ] } }); if (formValues) { fetch("eventHandler.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ request_type:'addEvent', start:start.startStr, end:start.endStr, event_data: formValues}), }) .then(response => response.json()) .then(data => { if (data.status == 1) { Swal.fire('Event added successfully!', '', 'success'); } else { Swal.fire(data.error, '', 'error'); } calendar.refetchEvents(); }) .catch(console.error); } }, eventClick: function(info) { info.jsEvent.preventDefault(); info.el.style.borderColor = 'red'; Swal.fire({ title: info.event.title, icon: 'info', html:'<p>'+info.event.extendedProps.description+'</p><a href="'+info.event.url+'">Visit event page</a>', showCloseButton: true, showCancelButton: true, cancelButtonText: 'Close', confirmButtonText: 'Delete Event', }).then((result) => { if (result.isConfirmed) { fetch("eventHandler.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ request_type:'deleteEvent', event_id: info.event.id}), }) .then(response => response.json()) .then(data => { if (data.status == 1) { Swal.fire('Event deleted successfully!', '', 'success'); } else { Swal.fire(data.error, '', 'error'); } calendar.refetchEvents(); }) .catch(console.error); } else { Swal.close(); } }); } }); calendar.render(); }); Server-side Handler Script with PHP and MySQLThe following code snippet shows how to handle the List/View/Add/Delete functionality from the server-side using PHP and MySQL. Fetch Events (fetchEvents.php):
<?php The Add Event (eventHandler.php):
Delete Event (eventHandler.php):
<?php Build an Event Calendar with PHP using jQuery, Ajax, and MySQL ConclusionThis guide helps you to create an event calendar with PHP using the FullCalendar plugin. The example code shows how to integrate list/view/add/delete functionality with the FullCalendar library using PHP and MySQL. You can allow the user to manage events on a full-sized calendar dynamically. The functionality of the event calendar can be enhanced easily with FullCalendar as per your needs. Are you want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request If you have any questions about this script, submit it to our QA community - Ask Question |