So, I'm having a real hard time with Select2. I want users to be able to search through a select list and also create new tags when no search result is found. Creating new tags is not the problem. The main thing - and surprisingly overlooked in most forums - is to INSERT the news tags to the Database. I'm new to Select2, so I'd like to have a full example. And I'm using PHP on this project, so, if there is a way to do this with PHP it would be really helpful.
Here is my code:
$('#caracteristicas').select2({theme:'bootstrap', width:null, placeholder:'Selecione', allowClear:true})
<?php
$q = mysqli_query($con, "select id, descricao from caracteristicas");
if(mysqli_num_rows($q)>(0))
{
?>
<div class="form-group">
<label for="caracteristicas">Selecione um ou mais resultados</label>
<select name="caracteristicas[]" id="caracteristicas" class="form-control" multiple="multiple">
<option value="">Selecione</option>
<?php
while($linhas = mysqli_fetch_array($q)):
?>
<option value="<?=$linhas["id"]?>">
<?=strlen($linhas["descricao"])>50 ? substr($linhas["descricao"], 0, 50)."..." : $linhas["descricao"]?>
</option>
<?php endwhile;?>
</select>
</div>
<?php } mysqli_close($con);?>
Note: I'm using Select2 4.0