I'm trying to get data of events out of my database using jQuery and AJAX.
My database table:
TABLE `events` (
`event_id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
`event_title` VARCHAR(255) NOT NULL,
`event_desc` TEXT,
`event_location` VARCHAR(255) NOT NULL,
`event_requirements` TEXT DEFAULT NULL,
`event_date` DATETIME NOT NULL,
PRIMARY KEY (`event_id`)
My code (in a MVC architecture):
Controller:
function index()
{
$overview_model = $this->loadModel('Events');
$this->view->events = $overview_model->getEventTypes();
$this->view->render('events/index');
}
Model:
public function getEventTypes()
{
$sth = $this->db->prepare("SELECT * FROM events");
$sth->execute();
$events = array();
foreach ($sth->fetchAll() as $event) {
$events[$event->event_id] = new stdClass();
$events[$event->event_id]->event_title = $event->event_title;
$events[$event->event_id]->event_desc = $event->event_desc;
$events[$event->event_id]->event_location = $event->event_location;
$events[$event->event_id]->event_requirements = $event->event_requirements;
$events[$event->event_id]->event_date = $event->event_date;
}
return $events;
}
View:
<!-- start event type selection -->
<div class="event-selection">
<label>Choose an event:</label>
<select id="select-event-type">
<?php foreach ($this->events as $event) {
echo "<option value='" .$event->event_title. "'>" .$event->event_title."</option>";
}?>
</select>
<!-- I NEED HELP WITH THIS PART -->
<script>
$(document).ready(function() {
$('#select-event-type').change(function() {
$.ajax({
type:'post',
url:'',
data:'',
success: function(data)
{
$('#event-details').html(data);
}
});
});
});
</script>
<!-- Start event-details -> here the data is supposed to be displayed -->
<div id="event-details"></div> <!-- /#event-details -->
</div><!-- /.event-selection -->
My problem: I'm not sure what URL and data are in my case and I am unsure, if I have to use JSON or not. I am not getting any error messages. Everytime I change my selection in the drop down it loads the whole page into the #event-details div and not the desired database data.
I'm really new to this and I have never worked with AJAX before. Thank you so much for any kind of help.
UPDATE:
In the console all the event's data is shown now in json format (Thank you so so much for that!). But there is still nothing outputted and when I change the event in the selection the data in the console does not update!