javascript Fullcalendar - 在数据库中保存事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32522927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:24:38  来源:igfitidea点击:

Fullcalendar - save events in database

javascriptphpjqueryajaxfullcalendar

提问by nameless

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:

我有一个 fullcalendar 实例。当点击一天(触发dayClick-callback)时,会打开一个引导模式,用户可以在其中输入标题和开始/结束日期。单击确定后,提供的这些值将添加到日历中。这是代码:

 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.

现在的问题是:如何将事件直接保存到数据库中,然后加载它,那么我在哪里可以引入php代码,将事件保存到数据库中......问题,我为什么要问,是添加事件之间的站点永远不会重新加载,所以我无法检查 GET 或 POST-Parameters 或类似的东西......我可以用 AJAX 来做到这一点吗?如果是,如何?因为我对 AJAX 不是很熟悉。

回答by

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.

  1. 在模态触发器之后使用此 ajax。
  2. 在模态中获取标题、开始日期、结束日期等值,并在以下 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. 在 add_events.php 中将详细信息保存在 db 中

  4. 在您的主页中,使用此方法动态创建事件源

    function eventSourceCall(){
         eventSourceCall = [
            {
                url: 'events.php?status=absent',
                backgroundColor: 'red',
                borderColor: 'white',
                textColor: 'white',
                rendering: 'background',
                cache: false
            }
    
  5. 在 events.php 中执行选择操作并检索事件参数作为 json 编码对象并返回它们。

  6. 在日历功能中eventSources: eventSourceCall,添加此行以选择事件源。