0

我有以下代码,与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";
}
?>
4

2 回答 2

1

我不认为将 /test/test2.php 放入您的脚本中实际上包含并运行 test2.php。

尝试这个

$("#course").autocomplete("<?php include '/test/test2.php'; ?>", {
于 2013-08-30T23:39:53.953 回答
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"));
于 2014-01-10T04:38:04.570 回答