How to update database after cloning an event in fullcalendar using AJAX - javascript

I'm relatively new to programming and some help would be greatly appreciated.
I am using fullcalendar and have got it to the point where I can add, remove & drag and drop events. My last requirement is to be able to clone events for quicker management.
I am using the following code to clone events (taken from a previous post). Currently this does not save to the database:
eventClick: function (event, jsEvent, ui, view) {
if (!copyKey) return;
var eClone = {
id: event.id+1,
title: event.title,
tooling: event.tooling,
start: event.start,
end: event.end
};
$('#calendar').fullCalendar('renderEvent', eClone);
},
An example piece of code I have used to update my SQL database is here:
eventDrop:function(event)
{
var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
var title = event.title;
var tooling = event.tooling;
var id = event.id;
$.ajax({
url:"update.php",
type:"POST",
data:{title:title, tooling:tooling, start:start, end:end, id:id},
success:function()
{
calendar.fullCalendar('refetchEvents');
}
});
},
My main confusion is how to use AJAX for my clone function, also making sure to increment the id when a new event is created.

Had to make use of insert.php rather than update.php...
Final working code:
eventClick: function (event, jsEvent, ui, view)
{
if (!copyKey) return;
var eClone = {
id: event.id,
title: event.title,
tooling: event.tooling,
start: event.start,
end: event.end
};
$('#calendar').fullCalendar('renderEvent', eClone);
var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
var title = event.title;
var tooling = event.tooling;
var id = event.id;
$.ajax({
url:"insert.php",
type:"POST",
data:{title:title, tooling:tooling, start:start, end:end, id:id},
success:function()
{
$('#calendar').fullCalendar('renderEvent', eClone);
calendar.fullCalendar('refetchEvents');
}
});
},

Related

FullCalendar open bootstrap modal on eventClick

I have a bootstrap fullcalendar, when I click on an empty day, the modal which allows to save the events is displayed well, but when I click on an event to modify it, the modal which allows to modify the event, does't display.
I have a bootstrap fullcalendar, when I click on an empty day, the modal which allows to save the events is displayed well, but when I click on an event to modify it, the modal which allows to modify the event, does't display.
I don't know where the problem comes from
this is my code :
<div class="modal fade" id="PrestUpt" role="dialog" class="modal fade task-modal-single in" tabindex="-1" aria-labelledby="myLargeModalLabel" >
<div class="modal-dialog modal-lg">
<div class="modal-content data">
<div id="jalil" ></div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
// jQuery("#PrestUpt").modal('show');
var calendar = $('#calendar').fullCalendar({
locale: 'fr',
plugins: [ 'bootstrap', 'interaction', 'dayGrid', 'timeGrid' ],
firstDay: 1,
editable:true,
header:{
left:'prev,next today',
center:'title',
right:'month,agendaWeek,agendaDay'
},
events: 'event_load_new.php',
selectable:true,
selectHelper:true,
select: function (start, end, allDay) {
//do something when space selected
//Show 'add event' modal
// var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
var start = $.fullCalendar.formatDate(start, "Y-MM-DD");
document.getElementById('start').value = start;
$('#exampleModal').modal('show');
},
editable:true,
eventResize:function(event)
{
var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
var title = event.title;
var id = event.id;
$.ajax({
url:"event_update.php",
type:"POST",
data:{title:title, start:start, end:end, id:id},
success:function(){
calendar.fullCalendar('refetchEvents');
alert("Mise à jour de l'événement");
}
})
},
eventClick: function(info) {
var idpr = info.event.id;
alert(idpr);
// datepr = datepr.substr(6, 4)+ '' +datepr.substr(3, 2)+ '' +datepr.substr(0, 2);
$.ajax({
type : 'post',
url : 'moadal_gool.php', //Here you will fetch records
data : 'idp='+idpr, //Pass $id
success : function(data){
$('#jalil').html(data);//Show fetched data from database
jQuery("#PrestUpt").modal('show');
}
});
},
eventDrop:function(event)
{
var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
var title = event.title;
var id = event.id;
$.ajax({
url:"event_update.php",
type:"POST",
data:{title:title, start:start, end:end, id:id},
success:function()
{
calendar.fullCalendar('refetchEvents');
alert("Événement mis à jour");
}
});
},
});
});
</script>
This is how you do it...
eventClick: this.openModal.bind(this)
//then create a function openModal()
openModal(args:any){
jQuery("#PrestUpt").modal("show");
...
}

show same month/week/day upon refresh of fullcalendar

I am using a fullcalendar plugin to display various events.
My problem is when I change the month/week being viewed, then refresh, I get taken back to the current month/week, when I want to stay on the same month/week I was viewing beforehand. i.e. If it's August right now, and I go back to July, then refresh, I want the page to display the calendar for July and not August.
Code that gives a working calendar for the problem specified is given below. Note that I understand this calendar doesn't fully work (can't add events etc.), but it saving it as index.html and opening it will show the problem I have of not opening at the previous view after a refresh.
<!DOCTYPE html>
<html>
<head>
<title>Jquery Fullcalandar Integration with PHP and Mysql</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.4.0/fullcalendar.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.4.0/fullcalendar.min.js"></script>
<script>
$(document).ready(function() {
var calendar = $('#calendar').fullCalendar({
allDaySlot: false,
editable:true,
header:{
left:'prev,next today',
center:'title',
right:'month,agendaWeek,agendaDay'
},
events: [{"id":1,"title":"one","start":"2019-08-11 00:00:00","end":"2019-08-12 00:00:00", 'color': 'Fuchsia'}],
selectable:true,
selectHelper:true,
select: function(start, end, allDay)
{
var title = prompt("Enter Event Title");
if(title)
{
var start = $.fullCalendar.formatDate(start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(end, "Y-MM-DD HH:mm:ss");
$.ajax({
url:"insert.php",
type:"POST",
data:{title:title, start:start, end:end},
success:function()
{
calendar.fullCalendar('refetchEvents');
alert("Added Successfully");
}
})
}
},
editable:true,
eventResize:function(event)
{
var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
var title = event.title;
var id = event.id;
$.ajax({
url:"update.php",
type:"POST",
data:{title:title, start:start, end:end, id:id},
success:function(){
calendar.fullCalendar('refetchEvents');
alert('Event Update');
}
})
},
eventDrop:function(event)
{
var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
var title = event.title;
var id = event.id;
$.ajax({
url:"update.php",
type:"POST",
data:{title:title, start:start, end:end, id:id},
success:function()
{
calendar.fullCalendar('refetchEvents');
alert("Event Updated");
}
});
},
eventClick:function(event)
{
if(confirm("Are you sure you want to remove it?"))
{
var id = event.id;
$.ajax({
url:"delete.php",
type:"POST",
data:{id:id},
success:function()
{
calendar.fullCalendar('refetchEvents');
alert("Event Removed");
}
})
}
},
});
});
</script>
</head>
<body>
<br />
<h2 align="center">Jquery Fullcalandar Integration with PHP and Mysql</h2>
<br />
<div class="container">
<div id="calendar"></div>
</div>
</body>
</html>
I wish to add a property somewhere (presumably after line 14 var calendar = $('#calendar').fullCalendar({ ) that makes it so after a refresh, I end up on the month/week I was previously on.
You can add this code to solve your problem
$(document).on('click', '.fc-next-button,.fc-prev-button', function () {
localStorage.setItem('savedMonth',$('#calendar').fullCalendar('getView').intervalStart._d);
});
if(localStorage.getItem('savedMonth')!=null){
$('#calendar').fullCalendar('gotoDate',localStorage.getItem('savedMonth'));
}
or you can do this using cookies instead of local storage

Javascript error when trying to create a calendar

I'm brand new to javascript and php and having a slight problem.
I'm trying to create a php calendar that interacts with mysql database and have been trying to write some javascript to add events to the calendar.
My events are being brought from the mysql database but whenever I click to add an event to the calendar I get an error in the console saying "uncaught TypeError: undefined is not a function" which appears to be caused by these lines in the code:
var start = $.fullCalendar.formatDate(start, "yyyy-MM-dd HH:mm:ss");
var end = $.fullCalendar.formatDate(end, "yyyy-MM-dd HH:mm:ss");
My code is below, console log 'testing function success' is not being displayed. Please help!
$(document).ready(function() {
//get current date
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var calendar = $('#calendar').fullCalendar({
theme:true,
weekNumbers: true,
//allow a 'more' option if too many events.
eventLimit: true,
//enables agenda view
header:{
left:'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
events: 'event.php',
selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
console.log('testing function 1');
var title = prompt('Event Title:');
if (title) {
var start = $.fullCalendar.formatDate(start, "yyyy-MM-dd HH:mm:ss");
var end = $.fullCalendar.formatDate(end, "yyyy-MM-dd HH:mm:ss");
$.ajax({
url: 'eventadd.php',
data: 'title='+ title+'&start='+ start +'&end='+ end ,
type: "POST",
success: function(json) {
console.log('testing function success');
alert('OK');
}
});
calendar.fullCalendar('renderEvent',
{
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
}
calendar.fullCalendar('unselect');
},
});
});
Instead of lines
var start = $.fullCalendar.formatDate(start, "yyyy-MM-dd HH:mm:ss");
var end = $.fullCalendar.formatDate(end, "yyyy-MM-dd HH:mm:ss");
use (take care not to redefine local varialbes start, end, which are params of the function, also use Moment's format function according to: http://momentjs.com/docs/#/displaying/format/ as recommended by the fullCalendar v2 code migration):
var sfmated = start.format("yyyy-MM-dd HH:mm:ss");
var efmated = end.format("yyyy-MM-dd HH:mm:ss");
a working jsfiddle is here:
http://jsfiddle.net/xv5n1wod/12/

How to Reload JSON data in Fullcalendar?

I am using Fullcalendar.
In my case I am adding Custom Events, Like when user clicks on specific date, he can add his event, when he is done with insertion, the custom event he just added show automatically appear on calendar, means the fresh JSON Data should reload and appear.
Here is my code:
events : "ajax/response.php",//Fetching JSON From PHP File//
function insertEventSchedule()
{
var title = $('#titl').val();
var fromDate = $('#fromDate').val();
var toDate = $('#toDate').val();
$.ajax({
type: "POST",
url : "ajax/insert_schedule_event.php",
data: "title="+title+"&fromDate="+fromDate+"&toDate="+toDate+"&timeStamp="+$.now(),
cache: false,
beforeSend: function()
{
$('.submit_data').html('<img src="include/content/loaders/ajax-loader.gif" width="20">');
},
success: function(html)
{
$('#myPopup').dialog('close');
}
});
}
}
You have to build an event Object and render that event using the renderEvent method before or after closing your dialog
UPDATE
var newEvent = new Object();
newEvent.title = "some text";
newEvent.start = new Date();
newEvent.allDay = false;
// ...
$('#calendar').fullCalendar( 'renderEvent', newEvent );
The complete list of event attribute are here

Ajax submit of data array in CakePHP not working

I'm making making a scheduling calendar where schedulers can drag and drop members of an organization onto Fullcalendar. I have gotten as far as storing dropped events in an array Object, arrayOfEvents. I want to make an Ajax submit to a CakePHP add function to insert the data into my events database and then reload the calendar. Here's my code.
External events to drop:
/* initialize the external events
-----------------------------------------------------------------*/
$('#external-events div.external-event').each(function() {
// create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
// it doesn't need to have a start or end
var eventObject = {
title: $.trim($(this).text()), // use the element's text as the event title
userId: this.id
};
// store the Event Object in the DOM element so we can get to it later
$(this).data('eventObject', eventObject);
// make the event draggable using jQuery UI
$(this).draggable({
zIndex: 999,
revert: true, // will cause the event to go back to its
revertDuration: 0 // original position after the drag
});
});
Fullcalendar:
/* initialize the calendar
-----------------------------------------------------------------*/
var arrayOfEvents = [];
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'agendaWeek,agendaDay'
},
defaultView: 'agendaWeek',
editable: true,
events: [
<?php foreach ($users as $user) {
foreach ($user['Event'] as $event):
?>
{
start: '<?php echo $event['start_date']; ?>',
end: '<?php echo $event['end_date']; ?>',
title: '<?php echo $event['title']; ?>',
allDay: false,
color: '#077169'
},
<?php endforeach;
}
?>
],
droppable: true, // this allows things to be dropped onto the calendar !!!
drop: function(date, allDay) { // this function is called when something is dropped
// retrieve the dropped element's stored Event Object
var originalEventObject = $(this).data('eventObject');
// we need to copy it, so that multiple events don't have a reference to the same object
var copiedEventObject = $.extend({}, originalEventObject);
// assign it the date that was reported
copiedEventObject.start = date;
copiedEventObject.end = (date.getTime() + 3600000) / 1000; // default shift length is 1 hour
copiedEventObject.userId = originalEventObject.userId;
copiedEventObject.allDay = false;
// render the event on the calendar
// the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
$('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
// Push events into array
arrayOfEvents.push(copiedEventObject);
//todo: the calendar needs to insert events into the event calendar.
console.log(arrayOfEvents);
}
});
});
Update Schedule function called on a link:
function updateSchedule()
{
var arrayOfEvents = [];
//var data = "numOfEvents=" + arrayOfEvents.length; //original
var data = "numOfEvents=" + arrayOfEvents.length;
// You can get all events out of the array here
for (var i = 0; i < arrayOfEvents.length; i++) {
var event = arrayOfEvents[i];
data += "&id" + i + "=" + event.id
+ "&start" + i + "=" + event.start
+ "&end" + i + "=" + event.end;
}
// Make your ajax post here
$.ajax({
type: "POST",
url: "<?php echo $this->webroot; ?>events/add",
data: data,
success: function(response) {
alert('done!');
},
fail: function(response) {
alert("error");
}
});
Cakephp events/add function:
public function add() {
$this->autoRender = false;
if ($this->RequestHandler->isAjax()) {
Configure::write('debug', 0);
}
if (!empty($this->data)) {
if ($this->Event->save($this->data)) {
echo 'Record has been added';
} else {
echo 'Error while adding record';
}
}
}
I know this is a long one but any input would be well received.

Categories

Resources