Administrators Nathan Posted July 11, 2012 Administrators Share Posted July 11, 2012 I'm not sure what to call what I'm trying to do so hard to run a Google search on it. I have a spot where I want to assign team members to a project. There is an input box where I can type their names. As I start typing their name I would like it to query the users table and show possible results, then I can click their name and they are added, then I can start typing another persons name and add multiple people. Quote Link to comment Share on other sites More sharing options...
Administrators Nathan Posted July 11, 2012 Author Administrators Share Posted July 11, 2012 An example of this is something like when you add admins to your Facebook Fan page. Quote Link to comment Share on other sites More sharing options...
Thomas Posted July 11, 2012 Share Posted July 11, 2012 I understand what you are trying to do, but I don't understand what your asking. Are you looking for a way to script it? Quote Link to comment Share on other sites More sharing options...
Administrators Nathan Posted July 11, 2012 Author Administrators Share Posted July 11, 2012 Yeah I need a tutorial on how to do it. Quote Link to comment Share on other sites More sharing options...
Administrators Nathan Posted July 11, 2012 Author Administrators Share Posted July 11, 2012 Pieced this code together so far, but not working. Am I missing some syntax somewhere? test.php page: <?php define("_VALID_PHP", true); require_once("init.php"); ?> <?php include("header.php");?> <meta charset="utf-8"> <style> .ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; } </style> <script> $(function() { function split( val ) { return val.split( /,\s*/ ); } function extractLast( term ) { return split( term ).pop(); } $( "#birds" ) // don't navigate away from the field on tab when selecting an item .bind( "keydown", function( event ) { if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) { event.preventDefault(); } }) .autocomplete({ source: function( request, response ) { $.getJSON( "search.php", { term: extractLast( request.term ) }, response ); }, search: function() { // custom minLength var term = extractLast( this.value ); if ( term.length < 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; } }); }); </script> <div class="demo"> <div class="ui-widget"> <label for="birds">Birds: </label> <input id="birds" size="50" /> </div> </div><!-- End demo --> <div class="demo-description"> <p>Usage: Enter at least two characters to get bird name suggestions. Select a value to continue adding more names.</p> <p>This is an example showing how to use the source-option along with some events to enable autocompleting multiple values into a single field.</p> </div><!-- End demo-description --> Search.php Page the code above calls to query: <?php define("_VALID_PHP", true); require_once("init.php"); //Connect to the Database require_once("lib/config.ini.php"); $connect = mysql_connect($host,$username,$password); mysql_select_db($database, $connect); $term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends $qstring = "SELECT username as value FROM users WHERE username LIKE '%".$term."%'"; $result24 = mysql_query($qstring) ;//query the database for entries containing the term while ($row24 = mysql_fetch_array($result24,MYSQL_ASSOC))//loop through the retrieved values { $row24['value']=htmlentities(stripslashes($row24['value'])); $row24['id']=(int)$row24['id']; $row_set[] = $row24;//build an array } echo json_encode($row_set);//format the array into json data ?> Quote Link to comment Share on other sites More sharing options...
Administrators Nathan Posted July 12, 2012 Author Administrators Share Posted July 12, 2012 Whoa! Well a full day later I finally got it all figured out. Turns out there were a few errors. I had to define the variable $row_set before my loop in search.php. Also within search.php I was not selecting the primary key for my table to fill the ID spot. So added that to the query and now it's working! http://Digitize Design.net/autocomp/test.php <?php define("_VALID_PHP", true); require_once("init.php"); //Connect to the Database require_once("lib/config.ini.php"); $connect = mysql_connect($host,$username,$password); mysql_select_db($database, $connect); $term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends $qstring = "SELECT username as value FROM users WHERE username LIKE '%".$term."%'"; $result24 = mysql_query($qstring) ;//query the database for entries containing the term $row_set = array(); while ($row24 = mysql_fetch_array($result24,MYSQL_ASSOC))//loop through the retrieved values { $row24['value']=htmlentities(stripslashes($row24['value'])); $row24['id']=(int)$row24['id']; $row_set[] = $row24;//build an array } echo json_encode($row_set);//format the array into json data ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.