gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:13:25 25 4
gpt4 key购买 nike

我正在尝试从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com