简体   繁体   English

无法使用PHP JSON在Javascript中以全日历显示来自MySQL的事件

[英]Unable to show events from MySQL in fullcalendar in Javascript using PHP JSON

I am trying to retrieve data from MySQL and display in full calender in my Javascript code. 我正在尝试从MySQL检索数据并以我的Javascript代码全屏显示。

Below is the code in my Javascript: 以下是我的Javascript中的代码:

$('#calendar').fullCalendar({
editable: true, 
header: {
left: 'prevYear,prev,next,nextYear today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
slotMinutes: 30,
allDaySlot: true,
selectable: true,
height: 580,
minTime: 10,
maxTime: 22,
selectHelper: true,
defaultview: 'week',
events: {
    url: '.././pages/json-events.php',
    type: 'POST',
    error: function() {
        alert('There was an error while fetching events.'); // Error alert
    }
},
timeFormat: 'hh:mmtt',
dayClick: function(date, allDay, jsEvent, view) {

if (allDay) {
alert('Clicked on the entire day: ' + date);
}else{
    //var slotDate=date.getDate() +'/'+date.getMonth()+'/'+date.getFullYear()+' '+date.getHours()+':'+date.getMinutes()
    //alert('Clicked on the slot: ' + slotDate);
    newappointment(date, allDay);
}
},

eventClick: function(calEvent, jsEvent, view) {
    //alert('Event title: ' + calEvent.title +' ; Event start:' + calEvent.start);
    editappointment(calEvent);
},

Below is my PHP code: 以下是我的PHP代码:

$con=mysqli_connect("server","user","pwd","db");
                // Check connection
                if (mysqli_connect_errno()) {
                  echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }

                $result = mysqli_query($con,"SELECT * FROM table  ORDER BY appId");

            /*  while($row = mysqli_fetch_array($result)) {
                  echo $row['appid'] . " " . $row['title'];
                  echo "<br>";
                }*/

                mysqli_close($con);

                // Returning array
                 $return_array = array();
                 // Event array
                 $event_array;
        while($row = mysqli_fetch_array($result)) {
        // while ($row = $sth->fetch(PDO::FETCH_ASSOC) {
            // Create a new array for the event
            $event_array = array();
            // Add data from database to the event array
            $event_array['id'] = $row['appId'];
            $event_array['title'] = $row['start'];;
            $event_array['start'] = new Date($row['startYear'],$row['startMonth'],$row['startDate'],$row['startHour'],$row['startMins']);
            $event_array['end'] = new Date($row['endYear'],$row['endMonth'],$row['endDate'],$row['endHour'],$row['endMins']);
            $event_array['allDay'] = false;
            // Merge the event array into the return array
            array_push($return_array, $event_array);
         }
         // Output the json feed as we need it to render the calendar
         echo json_encode($return_array);
    }

Every time the output come with the error message : 每次输出附带错误消息:

"There was an error while fetching events." “获取事件时出错。” Which is specified in my Javascript. 这是在我的Javascript中指定的。

What am I doing wrong? 我究竟做错了什么?

Try to replace: 尝试更换:

url: '.././pages/json-events.php',

with: 与:

url: '../pages/json-events.php',

You have also to move the mysqli_close($con); 您还必须移动mysqli_close($con); in PHP code after the end of the while cycle. 在while循环结束后使用PHP代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM