I want to apply "Autocomplete JQuery UI with multiple values" in the input field of registration forms.
What I want to do exactly: when the visitor name for an existing user in this input field, first of all, the script looks for the name to exist, terminates it (if exists), adds a comma. The user can enter the second, third ... existing user names in this field, and each time the script will be automatically completed. And when the client clicks the submit button, PHP searches for the identifier of these user names, creates an array of identifiers, adds it to the new friends field of friends "friends" in the db table.
My code is:
HTML
<form action="index.php" method="post"> <input class="std" type="text" name="friends" id="friends"/> <input type="submit" name="submit"> </form>
Jquery
$(function() { function split( val ) { return val.split( /,\s*/ ); } function extractLast( term ) { return split( term ).pop(); } $( "#friends" )
This is the original php file from the examples folder, which works great. but I want to get from the database instead of an array
Original search.php
$q = strtolower($_GET["term"]); if (!$q) return; $items = array( "Great Bittern"=>"Botaurus stellaris", "Little Grebe"=>"Tachybaptus ruficollis", "Black-necked Grebe"=>"Podiceps nigricollis", "Little Bittern"=>"Ixobrychus minutus", "Black-crowned Night Heron"=>"Nycticorax nycticorax", "Purple Heron"=>"Ardea purpurea", "White Stork"=>"Ciconia ciconia", "Spoonbill"=>"Platalea leucorodia", "Red-crested Pochard"=>"Netta rufina", "Common Eider"=>"Somateria mollissima", "Red Kite"=>"Milvus milvus", ); 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 => $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=>$value) { if (strpos(strtolower($key), $q) !== false) { array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key))); } if (count($result) > 11) break; } echo array_to_json($result);
Changed search.php
$conn = mysql_connect("localhost", "tural", "0579ural") or die( mysql_error() );; mysql_select_db("askon", $conn) or die( mysql_error() );; $q = strtolower($_GET["term"]); if (!$q) return; $query = mysql_query("select id, fullname from usr_table where fullname like '$q%'") or die( mysql_error() );; $results = array(); while ($row = mysql_fetch_array($query)) { $results[] = array( $row[1] => $row[0] ); } echo json_encode($results);
Autocomplete php script works prntscr.com/22mxl , but I think something is wrong with jquery: it does not display the menu. prntscr.com/22mxg . How to solve this problem? PS FOR ORIGINAL SEARCH. PHP IT RETURNS is something like this prntscr.com/22n0e and shows prntscr.com/22n0r .