javascript - 无法使用 PHP JSON 在 Javascript 中以 fullcalendar 显示来自 MySQL 的事件

标签 javascript php jquery mysql json

我正在尝试从 MySQL 检索数据并在我的 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);
},

下面是我的 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);
    }

每次输出都带有错误消息:

"There was an error while fetching events." Which is specified in my Javascript.

我做错了什么?

最佳答案

尝试替换:

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

与:

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

您还必须在 while 循环结束后移动 PHP 代码中的 mysqli_close($con);

关于javascript - 无法使用 PHP JSON 在 Javascript 中以 fullcalendar 显示来自 MySQL 的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23863254/

相关文章:

javascript - 将 js 变量发送到 php .ajax

javascript - 选择该颜色的选择列表选项时如何更改文档背景颜色

php - 如何将 ISO8601 TSQL DATETIME 参数与 PDO 绑定(bind)?

jquery propertychange 数组中存在 ID 的字段

jquery-mobile 面板将值传递给 Ajax

javascript - 按一次迭代后停止的日期值对 JSON 数组进行排序

javascript - 需要 Font Awesome 5 支持 DHTMLX 中带有 JS 的 SVG

php - 使用 php 在其他站点上运行 javascript

php - 从远程主机使用 google app engine 发送电子邮件,如何附加文件?

javascript - jQuery 迭代次数过多导致函数崩溃