EDIT: I'm not sure if anybody else has this problem, but here is how I solved it. My JSFiddle did work, and the data was displayed, but it wasn't selectable (on my localhost, where I got the data with JSON). I fixed it by changing the html code to the following:
<select class='candidateTags' id='candidateTags' data-placeholder='begin met typen voor suggesties' multiple='multiple' style='width: 200px;'></select>
So basically, I changed the input type hidden to select, and I added name='multipleTagids[ ]' to the select field.
If anybody else has this problem, then now you know how to fix it.
I know this question has been asked a lot (among others: here, here and here)
But the problem I'm having is that I'm getting data from a database and then encoding it with PHP to make a JSON object. This all works perfectly fine.
Select2 then uses the results to autocomplete

The problem is that when I try to click on one of the tags, the tag is not selected. When I click it, it just closes the autocomplete list.
Here's my select2 code:
<script>
$(document).ready(function() {
// set select option to be select2, and add a placeholder
$('.candidateTags').select2({
ajax: {
url: 'snippets/gettags.php', // the url that AJAX should access
dataType: 'json', // datatype that's received
data: function (term, page) {
return term;
},
id: function(obj) {
return obj.slug;
},
results: function (data, page) {
return {results: data.results};
}
}
});
});
</script>
I am using the id, but it doesn't work. I also tried not including this id part, but it still didn't work.
JSON is returning this:
{"results":[{"id":"1","text":"Project medewerker"},{"id":"2","text":"Vakkenvuller"},{"id":"3","text":"Developer"}]}
And here's the html:
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
<label>
<span>Tag(s):</span>
<!-- Set a select option for the tags. Will be filled with jQuery onkeyup='suggestTags(this.value)' -->
<input type='hidden' class="candidateTags" name='multipleTagIds[]' id='candidateTags' data-placeholder='Begin met typen voor suggesties' multiple='multiple'>
</label>
<input type="submit" name="submit" class="submitbutton" value="VERSTUREN" >
</form>