2011-06-16 70 views
6

Bir kayıt formları giriş alanına "Birden çok değer içeren JQuery UI Otomatik Tamamlama" uygulamak istiyorum.Otomatik Tamamlama düzgün çalışmıyor

Tam olarak ne yapmak isterim: Ziyaretçi, mevcut kullanıcının adını bu giriş alanına yazdığında, her şeyden önce, komut dosyası ad varoluşunu arar, tamamlar (varsa), virgül ekler. Kullanıcı bu alana ikinci, üçüncü ... mevcut kullanıcı adlarını yazabilir ve her zaman komut dosyası otomatik olarak tamamlanacaktır. Ziyaretçinin gönder düğmesini tıkladığında, PHP bu kullanıcı adının kimliğini arar, bir dizi kimlik oluşturur ve bunu db tablosundaki yeni kullanıcılara "arkadaşlar" alanına ekler.

Kodum:

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") 
     // 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; 
      } 
     }); 
}); 

bu mükemmel çalışıyor örnek klasöründen orijinal php dosyasıdır. ama veritabanından yerine
Orijinal dizide 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); 

Değiştirilen search.php getirme istiyorum

$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); 

Otomatik Tamamlama php komut prntscr.com/22mxl çalıştığı ama jquery ile yanlış bir şey düşünmek : Menüyü göstermiyor. prntscr.com/22mxg. bu sorunu nasıl çözeriz? ORİJİNAL SEARCH.PHP ITS İÇİN SAYFALAR Bu gibi bir şey prntscr.com/22n0e ve prntscr.com/22n0r gösterir.

+0

json_encode ($ results [0]); Firebug’ta kontrol et, cevabın hiç cevap almıyor ya da hiç bir şey yapmıyorsun. –

+0

no. search.php düzgün çalışıyor. jquery kısmında bir şey değiştirmeliyim. lütfen ekran görüntülerini açın ve orijinal dosya arasındaki farkı göreceksiniz ve –

+0

değiştirdim: değiştirdim $ results [] = array ("id" => $ row [0], "label" => $ row [1], "value" => $ row [1]); ama şimdi başka bir problem: sadece kullanıcı ve isminin kimliğine ihtiyacım var. değere ihtiyacım yok. Bu bölüm jQuery'den nasıl silinir? –

cevap