Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Seems this question hasn't been answered for a long time, I bet you have already found a solution but for those who haven't here is my answer:</p> <p>The indention got all messed up when i copied from my code ;)</p> <pre><code>&lt;input type="hidden" name="tags" id="mySingleField" value="Apple, Orange" disabled="true"&gt; Tags:&lt;br&gt; &lt;ul id="mytags"&gt;&lt;/ul&gt; &lt;script type="text/javascript"&gt; jQuery(document).ready(function() { jQuery("#mytags").tagit({ singleField: true, singleFieldNode: $('#mySingleField'), allowSpaces: true, minLength: 2, removeConfirmation: true, tagSource: function( request, response ) { //console.log("1"); $.ajax({ url: "search.php", data: { term:request.term }, dataType: "json", success: function( data ) { response( $.map( data, function( item ) { return { label: item.label+" ("+ item.id +")", value: item.value } })); } }); }}); }); </code></pre> <p></p> <p>and the "search.php" you can find this in some autocomplete jQuery examples actually.</p> <pre><code>&lt;?php $q = strtolower($_GET["term"]); if (!$q) return; $items = array( "Great Bittern"=&gt;"Botaurus stellaris", "Little Grebe"=&gt;"Tachybaptus ruficollis", "Black-necked Grebe"=&gt;"Podiceps nigricollis", "Little Bittern"=&gt;"Ixobrychus minutus", "Black-crowned Night Heron"=&gt;"Nycticorax nycticorax", "Heuglin's Gull"=&gt;"Larus heuglini" ); function array_to_json( $array ){ if( !is_array( $array ) ){ return false; } $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) )); if( $associative ){ $construct = array(); foreach( $array as $key =&gt; $value ){ // We first copy each key/value pair into a staging array, // formatting each key and value properly as we go. // Format the key: if( is_numeric($key) ){ $key = "key_$key"; } $key = "\"".addslashes($key)."\""; // Format the value: if( is_array( $value )){ $value = array_to_json( $value ); } else if( !is_numeric( $value ) || is_string( $value ) ){ $value = "\"".addslashes($value)."\""; } // Add to staging array: $construct[] = "$key: $value"; } // Then we collapse the staging array into the JSON form: $result = "{ " . implode( ", ", $construct ) . " }"; } else { // If the array is a vector (not associative): $construct = array(); foreach( $array as $value ){ // Format the value: if( is_array( $value )){ $value = array_to_json( $value ); } else if( !is_numeric( $value ) || is_string( $value ) ){ $value = "'".addslashes($value)."'"; } // Add to staging array: $construct[] = $value; } // Then we collapse the staging array into the JSON form: $result = "[ " . implode( ", ", $construct ) . " ]"; } return $result; } $result = array(); foreach ($items as $key=&gt;$value) { if (strpos(strtolower($key), $q) !== false) { array_push($result, array("id"=&gt;$value, "label"=&gt;$key, "value" =&gt; strip_tags($key))); } if (count($result) &gt; 11) break; } echo array_to_json($result); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload