Calendario icon indicating copy to clipboard operation
Calendario copied to clipboard

How to load data with AJAX?

Open pixelzdesign opened this issue 7 years ago • 1 comments

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.

pixelzdesign avatar Jun 26 '18 13:06 pixelzdesign

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();

opiums9 avatar Jun 22 '22 08:06 opiums9