Fullcalendar fetch data from php database

Asked

Viewed 3,086 times

2

I’m generating a calendar for a website, and I’m obligated to use the tool fullCalendar. The point is to fetch the events by php from my database. In js, the data is listed in the following demo.calendar.js file and contains the following::

;(function( $, window, document, undefined ) {
    $(document).ready(function() {

        // Full Calendar
        if( $.fn.fullCalendar ) {

            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();

            $("#mws-calendar").fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                editable: true,
                events: [{
                    title: 'All Day Event',
                    start: new Date(y, m, 1)
                }, {
                    id: 999,
                    title: 'Repeating Event',
                    start: new Date(y, m, d - 3, 16, 0),
                    allDay: false
                }, {
                    title: 'Meeting',
                    start: new Date(y, m, d, 10, 30),
                    allDay: false
                }]
            });
        }
    });

}) (jQuery, window, document);

Now I have a question, is there any way to fetch events by php? Or put this code in a php file? or header?

I’m doing: return the value by Jason, but so far without any success.

;(function( $, window, document, undefined ) {

    $(document).ready(function() {

        // Full Calendar
        if( $.fn.fullCalendar ) {

            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();

            $("#mws-calendar").fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                editable: true,
                events: './myfeed.php'
            });
        }
    });

}) (jQuery, window, document);

and in the file myfeed.php I have the following (it is an example without the BD query yet, I would just appear the events below, that the query is no longer needed):

<?php

   $year = date('Y'); 
   $month = date('m');

   echo json_encode(array( 

      array( 
         'id' => 1, 
         'title' => "Event1", 
         'start' => "$year-$month-20", 
         'end' => "$year-$month-22", 
         'url' => "http://google.com/" 
      ), 

      array( 
         'id' => 2, 
         'title' => "Event2", 
         'start' => "$year-$month-20", 
         'end' => "$year-$month-22", 
         'url' => "http://yahoo.com/" 
      ) 

   ));

?>

1 answer

1

Try to do it this way:

$.ajax({
type: "POST",
url: "myfeed.php",
data: {       //the data    },
success: function(events)
  {
          $('#mws-calendar').fullCalendar('removeEvents');
          $('#mws-calendar').fullCalendar('addEventSource', events);         
          $('#mws-calendar').fullCalendar('rerenderEvents' );
   }
 });

Also check that the myfeed.php file is in the same folder as the js file

  • but I delete everything that’s in demo.calendar.php and put that? yes, myfeed.php is on the same page. Thanks for the help!

  • in place of if after ready

  • so the calendar no longer appears

  • in your old code, does an error appear in the terminal? run the calendar with the element inspecter pressing F12, go to the network tab, and analyze if you are being consulted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.