我有以下代码,与test2.php
. 我的问题是我必须将菜单中选择的用户与其唯一 ID 链接起来,因此 URL 将是所选用户的页面。问题是 id 未定义。
我不知道为什么!我究竟做错了什么?来自的 URL
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";
}
?>