jQuery Tag-it plugin [duplicate]
-
25-05-2021 - |
Question
Possible Duplicate:
Trying to get tag-it to work with an AJAX call
How can I get tags from the database for autocomplete.
I thing I can handle the php part by myself :)
This is the minimal code for the autocomplete feature and I really don't get it how to post the tag to php.
$(function(){
$('#tags').tagit({
availableTags: ['c++', 'java', 'php', 'coldfusion', 'javascript', 'asp', 'ruby', 'python', 'c', 'scala', 'groovy', 'haskell', 'perl', 'erlang', 'apl', 'cobol', 'go', 'lua']
});
});
you can find the full code here: http://aehlke.github.com/tag-it/css/jquery.tagit.css
Solution
Do you want to fill in availableTags variable with available tags from the database? You can simply do
<?php
echo "'$tag1', ";
echo "'$tag2', ";
...
?>
between [
and ]
Or you can of course get your data with $.ajax()
or $.get()
For example:
$.get('available.php', function(data) {
console.dir(data);
});
In case you are doing autocomplete, you can have your PHP script respond with available tags by given entry, for example:
<?php
$response = Array();
foreach ($tags as $tag) {
if (strpos($tag, $entry) !== false) $response[] = $tag;
}
now echo $response array as json or xml or whatever you want
?>
On the JavaScript part you would invoke on each keypress a call similar to this:
$.ajax({
type: "POST",
url: "available.php",
data: "entry="+ $('input[name=entry]').val(),
}).done(function( data) {
// invoke your TagIt plugin with data but first decode it
});
OTHER TIPS
Use:
var availableTags = <?php echo json_encode($availableTags); ?>;
Here $availableTags
is a PHP array that you can create with
$availableTags = array();