0

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

Community
  • 1
  • 1
Dave
  • 114
  • 11

2 Answers2

1

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
});
lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
  • the first solution would echo all tags from the database...there are over 2000 tags in the database. the second code...would that work like a standard search? – Dave Mar 19 '12 at 15:06
  • I've updated the answer, thebasdfasd – lukas.pukenis Mar 19 '12 at 15:13
  • i don't get it really, I just have this code:
      so, how should it look like when all is done?
      – Dave Mar 19 '12 at 15:32
    • 1
      Does this http://stackoverflow.com/questions/6938802/trying-to-get-tag-it-to-work-with-an-ajax-call help ? – lukas.pukenis Mar 19 '12 at 16:42
    • I am glad that helped, however next time use Google as I've entered "tagit ajax stackoverflow" and the first result was that link :) – lukas.pukenis Mar 19 '12 at 16:48
    0

    Use:

    var availableTags = <?php echo json_encode($availableTags); ?>;
    

    Here $availableTags is a PHP array that you can create with

    $availableTags = array();
    
    Javier Brooklyn
    • 624
    • 3
    • 9
    • 25
    • Not a good idea at all because there could be thousands of tags in the database and the question is already solved here http://stackoverflow.com/questions/6938802/trying-to-get-tag-it-to-work-with-an-ajax-call – Dave Feb 04 '13 at 14:31
    • Yes that is the whole point, you get all the tags. Or is it that there needs to be a limit for the number of tags? And of course you can use array_unique to get the unique tags, its more faster to retrieve results than getting the tags from a php file. – Javier Brooklyn Feb 04 '13 at 15:00
    • As I mentioned, there are thousands of UNIQUE tags in the database...It would be very slow to get them all at once and you just need a couple for autocomplete. Got it? – Dave Feb 04 '13 at 17:15
    • Oh so you mean to keep adding tags one-by-one manually. Then that would work. I dont think google would want to do that. – Javier Brooklyn Feb 04 '13 at 17:26