jQuery Fullcalendar Integration with Bootstrap, PHP & MySQL
Recently I have used this plugin in an application and faced some issues to integrate it with bootstrap so I don’t want my readers to waste time in integration in this tutorial you will get a fullcalendar working with bootstrap, PHP & MySQL. We are doing some basic operations like add event, edit event and delete event.
Let’s start.
Step 1. Download fullcalendar plugin from here.
Step 2. Create database table events
1 2 3 4 5 6 7 | CREATE TABLE `events` ( `id` int(11) NOT NULL AUTO_INCREMENT, `start` datetime DEFAULT NULL, `end` datetime DEFAULT NULL, `title` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; |
Step 3. Code includes below jQuery scripts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!— jQuery —> <script src=“https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js”></script> <!— custom scripts —> <script type=“text/javascript” src=“js/script.js”></script> <!— bootstrap —> <script src=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js” crossorigin=“anonymous”></script> <link href=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css” rel=“stylesheet” > <!— fullcalendar —> <link href=“css/fullcalendar.css” rel=“stylesheet” /> <link href=“css/fullcalendar.print.css” rel=“stylesheet” media=“print” /> <script src=“js/moment.min.js”></script> <script src=“js/fullcalendar.js”></script> |
PHP Files and coding:
db.php
1 2 3 | <?php $connection = mysqli_connect(‘localhost’,‘DBUser’,‘DBPassword’,‘DBName’) or die(mysqli_error($connection)); ?> |
Add your database connection credntials.
index.php
This file contains all the operations like add, update, delete events and load event and modal popups of bootstrap.
You can seperate all operations in files as add-event.php, edit-event.php etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <?php include(“db.php”); if(isset($_POST[‘action’]) or isset($_GET[‘view’])) //show all events { if(isset($_GET[‘view’])) { header(‘Content-Type: application/json’); $start = mysqli_real_escape_string($connection,$_GET[“start”]); $end = mysqli_real_escape_string($connection,$_GET[“end”]); $result = mysqli_query($connection,“SELECT `id`, `start` ,`end` ,`title` FROM `events` where (date(start) >= ‘$start’ AND date(start) <= ‘$end’)”); while($row = mysqli_fetch_assoc($result)) { $events[] = $row; } echo json_encode($events); exit; } elseif($_POST[‘action’] == “add”) // add new event { mysqli_query($connection,“INSERT INTO `events` ( `title` , `start` , `end` ) VALUES ( ‘”.mysqli_real_escape_string($connection,$_POST[“title”]).“‘, ‘”.mysqli_real_escape_string($connection,date(‘Y-m-d H:i:s’,strtotime($_POST[“start”]))).“‘, ‘”.mysqli_real_escape_string($connection,date(‘Y-m-d H:i:s’,strtotime($_POST[“end”]))).“‘ )”); header(‘Content-Type: application/json’); echo ‘{“id”:”‘.mysqli_insert_id($connection).‘”}’; exit; } elseif($_POST[‘action’] == “update”) // update event { mysqli_query($connection,“UPDATE `events` set `start` = ‘”.mysqli_real_escape_string($connection,date(‘Y-m-d H:i:s’,strtotime($_POST[“start”]))).“‘, `end` = ‘”.mysqli_real_escape_string($connection,date(‘Y-m-d H:i:s’,strtotime($_POST[“end”]))).“‘ where id = ‘”.mysqli_real_escape_string($connection,$_POST[“id”]).“‘”); exit; } elseif($_POST[‘action’] == “delete”) // remove event { mysqli_query($connection,“DELETE from `events` where id = ‘”.mysqli_real_escape_string($connection,$_POST[“id”]).“‘”); if (mysqli_affected_rows($connection) > 0) { echo “1”; } exit; } } ?> |
HTML popup and calander div defined in below markup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <!— add calander in this div —> <div class=“container”> <div class=“row”> <div id=“calendar”></div> </div> </div> <!— Modal to Add Event —> <div id=“createEventModal” class=“modal fade” role=“dialog”> <div class=“modal-dialog”> <!— Modal content—> <div class=“modal-content”> <div class=“modal-header”> <button type=“button” class=“close” data–dismiss=“modal”>×</button> <h4 class=“modal-title”>Add Event</h4> </div> <div class=“modal-body”> <div class=“control-group”> <label class=“control-label” for=“inputPatient”>Event:</label> <div class=“field desc”> <input class=“form-control” id=“title” name=“title” placeholder=“Event” type=“text” value=“”> </div> </div> <input type=“hidden” id=“startTime”/> <input type=“hidden” id=“endTime”/> <div class=“control-group”> <label class=“control-label” for=“when”>When:</label> <div class=“controls controls-row” id=“when” style=“margin-top:5px;”> </div> </div> </div> <div class=“modal-footer”> <button class=“btn” data–dismiss=“modal” aria–hidden=“true”>Cancel</button> <button type=“submit” class=“btn btn-primary” id=“submitButton”>Save</button> </div> </div> </div> </div> <!— Modal to Event Details —> <div id=“calendarModal” class=“modal fade”> <div class=“modal-dialog”> <div class=“modal-content”> <div class=“modal-header”> <button type=“button” class=“close” data–dismiss=“modal”>×</button> <h4 class=“modal-title”>Event Details</h4> </div> <div id=“modalBody” class=“modal-body”> <h4 id=“modalTitle” class=“modal-title”></h4> <div id=“modalWhen” style=“margin-top:5px;”></div> </div> <input type=“hidden” id=“eventID”/> <div class=“modal-footer”> <button class=“btn” data–dismiss=“modal” aria–hidden=“true”>Cancel</button> <button type=“submit” class=“btn btn-danger” id=“deleteButton”>Delete</button> </div> </div> </div> </div> <!—Modal—> |
script.js
This file contains all the operations and send request to PHP and receive responce on fron.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | $(document).ready(function(){ var calendar = $(‘#calendar’).fullCalendar({ // assign calendar header:{ left: ‘prev,next today’, center: ‘title’, right: ‘agendaWeek,agendaDay’ }, defaultView: ‘agendaWeek’, editable: true, selectable: true, allDaySlot: false, events: “index.php?view=1”, // request to load current events eventClick: function(event, jsEvent, view) { // when some one click on any event endtime = $.fullCalendar.moment(event.end).format(‘h:mm’); starttime = $.fullCalendar.moment(event.start).format(‘dddd, MMMM Do YYYY, h:mm’); var mywhen = starttime + ‘ – ‘ + endtime; $(‘#modalTitle’).html(event.title); $(‘#modalWhen’).text(mywhen); $(‘#eventID’).val(event.id); $(‘#calendarModal’).modal(); }, select: function(start, end, jsEvent) { // click on empty time slot endtime = $.fullCalendar.moment(end).format(‘h:mm’); starttime = $.fullCalendar.moment(start).format(‘dddd, MMMM Do YYYY, h:mm’); var mywhen = starttime + ‘ – ‘ + endtime; start = moment(start).format(); end = moment(end).format(); $(‘#createEventModal #startTime’).val(start); $(‘#createEventModal #endTime’).val(end); $(‘#createEventModal #when’).text(mywhen); $(‘#createEventModal’).modal(‘toggle’); }, eventDrop: function(event, delta){ // event drag and drop $.ajax({ url: ‘index.php’, data: ‘action=update&title=’+event.title+‘&start=’+moment(event.start).format()+‘&end=’+moment(event.end).format()+‘&id=’+event.id , type: “POST”, success: function(json) { //alert(json); } }); }, eventResize: function(event) { // resize to increase or decrease time of event $.ajax({ url: ‘index.php’, data: ‘action=update&title=’+event.title+‘&start=’+moment(event.start).format()+‘&end=’+moment(event.end).format()+‘&id=’+event.id, type: “POST”, success: function(json) { //alert(json); } }); } }); $(‘#submitButton’).on(‘click’, function(e){ // add event submit // We don’t want this to act as a link so cancel the link action e.preventDefault(); doSubmit(); // send to form submit function }); $(‘#deleteButton’).on(‘click’, function(e){ // delete event clicked // We don’t want this to act as a link so cancel the link action e.preventDefault(); doDelete(); send data to delete function }); function doDelete(){ // delete event $(“#calendarModal”).modal(‘hide’); var eventID = $(‘#eventID’).val(); $.ajax({ url: ‘index.php’, data: ‘action=delete&id=’+eventID, type: “POST”, success: function(json) { if(json == 1) $(“#calendar”).fullCalendar(‘removeEvents’,eventID); else return false; } }); } function doSubmit(){ // add event $(“#createEventModal”).modal(‘hide’); var title = $(‘#title’).val(); var startTime = $(‘#startTime’).val(); var endTime = $(‘#endTime’).val(); $.ajax({ url: ‘index.php’, data: ‘action=add&title=’+title+‘&start=’+startTime+‘&end=’+endTime, type: “POST”, success: function(json) { $(“#calendar”).fullCalendar(‘renderEvent’, { id: json.id, title: title, start: startTime, end: endTime, }, true); } }); } }); |
A Demo and free demo code available for download with this tutorial
[youtube https://www.youtube.com/watch?v=pXwFs_i8M7Q?rel=0&showinfo=0&w=853&h=480]
This is the simple guide to add fullcalender events in your application and it’s super easy i hope you like this please feel free to share your reviews and problems in comments.
Tutorial Categories:
Tutorial Categories:
Nice Tutorial Thanks PHPGang
Like the way you explained this plugin thanks, one thing i want to know how can we edit title of any event you didn’t add any thing for that.
on event details you need to add input text and add an update button and a function like add event and run update operation.
Could this be used for appointment booking?
Yes you can use it.
Thanks for an excellent tutorial, can this be used for appointments? if not do you know of a similar appointment plugin?
Great tutorial – thanks
but having a major problem!!
When entering data lets say for 1 november to 8 december – it show event on the month november (even more into december) but clicking on next month it should show even up to 8 december but there’s nothing there – btw i work only in month view of the calendar ( trying to make a holiday booking system)
a second problem is it shows only the even upto the 7 december while in database given dat is 8 december so a day short
could you please help me out??
Hi I have downloaded jquery fullcalendar php mysql code but after making changes as explained it shows unexpected error in script.js at first line
please help
Please share error from console.
Unhandled exception at line 1, column 1 in http://127.0.0.1:17602/js/script.js
0x800a138f – Microsoft JScript runtime error: Object expected
this is the eroor which i am receiving
how to include combobox or listbox in event calendar like users selection from other table and save it into events table
how to include combobox or listbox in event calendar like users selection from other table and save it into events table
how to add more fields into event calendar please help
Hi, this is awesome code. I’ve set-upped whole code and everything is working fine. But my question is, Can I show this calendar in Monthly view. If yes than How?
hey can you send me the source code l seemm to have a little challenge
why cancel button is not function
Works for me!
Thereis no any db.sql file in downloaded folder
I put my email into it but still no download files downloaded
Hello, I’ve subscribed and downloaded the source code, I’m having a problem.
I tried to run the source code but it says like this:
Warning: mysqli_connect(): (HY000/1045): Access denied for user ‘DatabaseUser’@’localhost’ (using password: YES) in D:xamphtdocsPHPdatabase.php on line 2
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in D:xamphtdocsPHPdatabase.php on line 2
Please answer, its a urgent project for us and thanks if you do.
hello, i need some assistance, i am using this nice calendar but i am facing some issues
I’m trying to add recurring events using your example code, but I’m getting stuck… Any suggestions, or chance you’ll do a update for fullCalendar 4.x?