最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fullcalendar - save events in database - Stack Overflow

programmeradmin2浏览0评论

I have the following markup:

I have a instance of fullcalendar. When clicking on a day (triggering the dayClick-callback), a bootstrap modal is opened, where the user can enter a title, and the start/end date. Once clicking on ok, those values provided, will be added to the calendar. Here's the code for that:

 function addTitle(){ //having a button onClick="addTitle()"
            var title = $('#add_date_title').val();
            var startdate = $('#add_date_startdate').val();
            var enddate = $('#add_date_enddate').val();
            var end_split = enddate.split('-');
            end_split[2]= parseInt(end_split[2])+parseInt("1");
            enddate = end_split[0] + "-" + end_split[1] + "-" + end_split[2];
            $('#add_date_title').val('');
            $('#add_date_startdate').val('');
            $('#add_date_enddate').val('');
            $('#add_date_modal').modal('hide');

            var myCalendar = $('#calendar');
            var myEvent = {
                title:title,
                allDay: true,
                start: startdate,
                end: enddate
            };
            myCalendar.fullCalendar( 'renderEvent', myEvent );
        }

So the event is now in the calendar. But when e.g. switching the month, or reloading the page, all data is lost, of course, because it's saved nowhere.

Now the question is: How could I save the event directly into the database, and then load it, so where can I bring in php code, to save the event to a db... The problem, why I'm asking, is that the site in between adding events is never reloaded, so I'm not able, to check for GET or POST-Parameters or something similiar... Could I maybe do this with AJAX? If yes, how? Because I'm not really familiar with AJAX.

I have the following markup:

I have a instance of fullcalendar. When clicking on a day (triggering the dayClick-callback), a bootstrap modal is opened, where the user can enter a title, and the start/end date. Once clicking on ok, those values provided, will be added to the calendar. Here's the code for that:

 function addTitle(){ //having a button onClick="addTitle()"
            var title = $('#add_date_title').val();
            var startdate = $('#add_date_startdate').val();
            var enddate = $('#add_date_enddate').val();
            var end_split = enddate.split('-');
            end_split[2]= parseInt(end_split[2])+parseInt("1");
            enddate = end_split[0] + "-" + end_split[1] + "-" + end_split[2];
            $('#add_date_title').val('');
            $('#add_date_startdate').val('');
            $('#add_date_enddate').val('');
            $('#add_date_modal').modal('hide');

            var myCalendar = $('#calendar');
            var myEvent = {
                title:title,
                allDay: true,
                start: startdate,
                end: enddate
            };
            myCalendar.fullCalendar( 'renderEvent', myEvent );
        }

So the event is now in the calendar. But when e.g. switching the month, or reloading the page, all data is lost, of course, because it's saved nowhere.

Now the question is: How could I save the event directly into the database, and then load it, so where can I bring in php code, to save the event to a db... The problem, why I'm asking, is that the site in between adding events is never reloaded, so I'm not able, to check for GET or POST-Parameters or something similiar... Could I maybe do this with AJAX? If yes, how? Because I'm not really familiar with AJAX.

Share Improve this question asked Sep 11, 2015 at 11:54 namelessnameless 1,5316 gold badges43 silver badges86 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

You can actually save the events in DB.

  1. Use this ajax after your modal trigger.
  2. Get the values like title,startdate, end date in modal and send them in the following ajax

    $.ajax({
      url: 'add_events.php',
      data: 'title='+ title+'&start='+ start2 +'&end='+ end2,                                                    
      type: "POST",
      success: function(json) {
        $( "#getReason" ).modal('hide');
        $('#mydiv').hide();
        $('body').removeClass('blockMask');//calendar.fullCalendar( 'refetchEvents');
        $('#calendar').fullCalendar('refetchEvents');
      }
    });
    
  3. in add_events.php save the details in db

  4. In your main page use this method for dynamically creating event source

    function eventSourceCall(){
         eventSourceCall = [
            {
                url: 'events.php?status=absent',
                backgroundColor: 'red',
                borderColor: 'white',
                textColor: 'white',
                rendering: 'background',
                cache: false
            }
    
  5. in events.php perform a select operation and retrieve the event parameters as json encoded objects and return them.

  6. In calendar function eventSources: eventSourceCall, add this line for selecting the event source.

发布评论

评论列表(0)

  1. 暂无评论