Calendario
Calendario copied to clipboard
How to load data with AJAX?
I need to load event data with AJAX for current month and when click on prev or next month to load data with ajax for those months.
In PHP example:
if($_GET['json']){
header('Content-Type: application/json');
$dbc = new DataBase();
$stmt = $dbc->connect()->prepare("SELECT * FROM `calendar` ORDER BY `Date` ASC");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$json = array();
if(!empty($result)){
foreach($result as $row){
$json[] = array(
$row['Date'] => array(
'content' => $row['content'],
'repeat' => $row['repeats'],
'allDay' => $row['allDay'],
'endDate' => $row['endDate'],
),
);
}
}
$dbc->disconnect();
echo json_encode($json);
exit();
}
and JS example:
function ajaxCalendar(){
$.get('hand.php', {json:1}, function(data){
var result = {};
data.forEach(function(v,i){
result[Object.keys(v)] = Object.values(v);
});
$('#calendar').calendario({
caldata: result,
displayWeekAbbr: true,
events: ['click', 'focus']
});
});
}
ajaxCalendar();