title: Jquery Ui autocomplete from DB
body: <p>I'm newbie to js. I found autocomplete tutorial and it works well.But autocomplete script configured for multiple values from db. It adds comma every time after found keyword then searchs for new keyword again. How to rewrite it for single value?</p>
  
  <p>acompl.js</p>
  
  <pre><code>$(function() {
      function split( val ) {
          return val.split( /,\s*/ );
      }
      function extractLast( term ) {
          return split( term ).pop();
      }
  
      $( "#region" )
          // don't navigate away from the field on tab when selecting an item
          .bind( "keydown", function( event ) {
              if ( event.keyCode === $.ui.keyCode.TAB &amp;&amp;
                      $( this ).data( "autocomplete" ).menu.active ) {
                  event.preventDefault();
              }
          })
          .autocomplete({
              source: function( request, response ) {
                  $.getJSON( "core/code/includes/search.php", {
                      term: extractLast( request.term )
                  }, response );
              },
              search: function() {
                  // custom minLength
                  var term = extractLast( this.value );
                  if ( term.length &lt; 2 ) {
                      return false;
                  }
              },
              focus: function() {
                  // prevent value inserted on focus
                  return false;
              },
              select: function( event, ui ) {
                  var terms = split( this.value );
                  // remove the current input
                  terms.pop();
                  // add the selected item
                  terms.push( ui.item.value );
                  // add placeholder to get the comma-and-space at the end
                  terms.push( "" );
                  this.value = terms.join( ", " );
                  return false;
              }
          });
  });
  </code></pre>
  
  <p>search.php</p>
  
  <pre><code>&lt;?php
  if ($term = @$_GET['term']) {
      require 'db.php';
      $q = $db-&gt;real_escape_string(strtolower($term).'%');
      $query = $db-&gt;query("SELECT id, region FROM regions WHERE region like '$q'") or die(mysqli_error());
      $results = array();
      while ($row = $query-&gt;fetch_row()) $results[] = array( 'id' =&gt; $row[0] , 'label' =&gt; $row[1], 'value' =&gt; $row[1] );
      echo json_encode($results);
  }
  ?&gt;
  </code></pre>