JQuery FullCalendar how to show data after ajax call without refreshing the page
I'm trying to integrate jQuery FullCalendar with PHP and MySQL .. The problem for me is that when I insert a new event picking days with AJAX, I don't know how to show this newly added event without refreshing the page. So it's basically a FullCalendar call:
var calendar = $('#calendar').fullCalendar({
editable: true,
firstDay: 1,
header: {
left: 'month, agendaWeek, agendaDay',
center: 'title',
right: 'prev, next, today'
},
// receive events from DB
events: {
url: 'events.php',
type: 'POST',
cache: false,
error: function() {
alert('ERROR');
}
},
buttonIcons: {
prev: 'left-single-arrow',
next: 'right-single-arrow'
},
// Convert the allDay from string to boolean
eventRender: function(event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = false;
}
element.find('.fc-event-title').append("<br/>" + event.description);
},
selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
var start = moment(start).format("YYYY-MM-DD HH:mm:ss");
var end = moment(end).format("YYYY-MM-DD HH:mm:ss");
var user_id = <?php echo $_SESSION['user_id']; ?>
// to unix
start = moment(start).unix();
end = moment(end).unix();
$('#myModal').modal({
remote: 'modals/add_event.php?start='+start+'&end='+end+'&id='+user_id
});
}
});
The place where I am doing the insert:
select: function(start, end, allDay) {
var start = moment(start).format("YYYY-MM-DD HH:mm:ss");
var end = moment(end).format("YYYY-MM-DD HH:mm:ss");
var user_id = <?php echo $_SESSION['user_id']; ?>
// to unix
start = moment(start).unix();
end = moment(end).unix();
$('#myModal').modal({
remote: 'modals/add_event.php?start='+start+'&end='+end+'&id='+user_id
});
}
This opens a modal dialog in which I post the calendar data and then post the data to the DB via AJAX. And after this point, I can't figure out how to show the newly added data without refreshing the page. Any help here?
Optional: This is my AJAX call when submitting a modal dialog form:
$.ajax({
type : "POST",
url : "ajax/add_event.php?start="+start.unix()+'&end='+end.unix()+'&id='+user_id,
data : $("#smart-form-event").serialize(),
success : function(data, status, xhr) {
$.smallBox({
title : "Event added successfully",
content : "<i class='fa fa-exclamation'></i> <i></i>",
color : "#659265",
iconSmall : "fa fa-check fa-2x fadeInRight animated",
timeout : 4000
});
$("#myModal").modal('hide');
}
});
Thank!
source to share
You can call "refetchEvents" after you introduce a new event http://fullcalendar.io/docs/event_data/refetchEvents/
$ ('# calendar') fullCalendar ('refetchEvents') ;.
...
success : function(data, status, xhr) {
$.smallBox({
title : "Event added successfully",
content : "<i class='fa fa-exclamation'></i> <i></i>",
color : "#659265",
iconSmall : "fa fa-check fa-2x fadeInRight animated",
timeout : 4000
});
$("#myModal").modal('hide');
$('#calendar').fullCalendar('refetchEvents');
}
...
source to share
Try adding return false;
$.ajax({
type : "POST",
url : "ajax/add_event.php?start="+start.unix()+'&end='+end.unix()+'&id='+user_id,
data : $("#smart-form-event").serialize(),
success : function(data, status, xhr) {
$.smallBox({
title : "Event added successfully",
content : "<i class='fa fa-exclamation'></i> <i></i>",
color : "#659265",
iconSmall : "fa fa-check fa-2x fadeInRight animated",
timeout : 4000
});
$("#myModal").modal('hide');
}
});
return false;
return false;
must be inside the dispatch function.
$('form').submit(function(e){
$ajax({...});
return false;
});
source to share
Please try this.
$ ('# calendar'). fullCalendar ('destroy'); $ ('# calendar'). fullCalendar ({ header: { left: 'prev, next today', center: 'title', strong text right: 'month, basicWeek, basicDay' //, agendaWeek, agendaDay }, defaultView: 'month', editable: true, events: myevents });
source to share