2013-08-30 33 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'; ?>", {