2013-08-30 30 views
0

我有以下代碼,這與test2.php有關。我的問題是,我必須將菜單中選擇的用戶與其唯一ID關聯起來,因此該URL將成爲所選用戶的頁面。問題是,該ID是未定義的。無法從MySQL中恢復值

而我不知道爲什麼!我究竟做錯了什麼?是來自

window.location.href = '/profile.php?id='+pieces[1]; 

網址爲/profile.php?id=undefined

$("#course").autocomplete("/test/test2.php", { 
     selectFirst: false, 
     formatItem: function(data, i, n, value) { 
      //make the suggestion look nice 
      return "<font color='#3399CC'>" + value.split("::")[0] + "</font>"; 
     }, 
     formatResult: function(data,value) { 
      //only show the suggestions and not the URLs in the list 
      return value.split("::")[0]; 
    var username = splitItems[0]; 
     var id = splitItems[1]; 
      return username; 
     } 
    }).result(function(event, data, formatted) { 
     //redirect to the URL in the string 
    var pieces = formatted.split("::"); 
     window.location.href = '/profile.php?id='+pieces[1]; 

test2.php

<?php 
mysql_connect ("****", "****","****") or die (mysql_error()); 
mysql_select_db ("****"); 
$q = strtolower($_GET["q"]); 
if (!$q) return; 
$sql = "select Username, id from **** where Username LIKE '%$q%'"; 
$rsd = mysql_query($sql); 
while($rs = mysql_fetch_array($rsd)) { 
    $cname = $rs['Username']; 
    echo "$cname\n"; 
} 
?> 
+0

嘗試記錄這些碎片,看看它有什麼'console.log(件)' – Sedz

+0

它沒有什麼... – freddy

回答

1

對於你的第一個代碼,你應該通過這個替換它:

$("#course").autocomplete("/test/test2.php", { 
     delay: 0, 
     selectFirst: false, 
     formatItem: function(data, i, n, value) { 
     //make the suggestion look nice 
     var pièces = value.split("::"); 
       return "<font color='#000000'; font size='3pt'; font face='Arial'>" + pièces[0] + "</font>"; 
     }, 
     formatResult: function(data,value) { 
      //only show the suggestions and not the URLs in the list 
     var pièces = value.split("::"); 

       return pièces[0]; 

     } 
    }).result(function(event, data, formatted) { 
    //redirect to the URL in the string 

    var pieces = formatted.split("::"); 
      window.location.href = '/profile.php?id='+pieces[1]; 

對於你的第二個代碼

您應該添加:

$id = $rs['id']; 

$cname = $rs['Username']; 

並添加此更改以下行。

echo (utf8_encode("$cname::$id\n")); 
1

我不認爲把/test/test2.php在腳本中實際上包含並運行test2.php。

試試這個

$("#course").autocomplete("<?php include '/test/test2.php'; ?>", {