在自动完成菜单中单独的网址

时间:2013-08-30 01:26:14

标签: php jquery mysql

我有一个自动完成的jQuery菜单,它从MySQL数据库输出我拥有的所有用户的名字。我正在尝试将每个选项链接到正确的配置文件。为此,URL类似于:/ profile.php?id = 341,341代表所选用户的ID。

唯一的问题是,当我尝试输入给定用户的ID时,所有用户的ID都显示在URL中......并且我只想要所选用户的ID!

我已尝试使用PHP,但我不知道要添加到以下行中的内容以使其正常工作。

$req = mysql_query("select id, Username, EmailAddress from ***");

应该是WHERE Username ='username'....最后,我知道我应该尝试别的东西,没有PHP,但我只是想以这种方式测试它!谢谢!

<input type="text" name="course" id="course" />

<script type="text/javascript" src="jquery.js"></script>
<script type='text/javascript' src='jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />

<script type="text/javascript">
$().ready(function() {

$("#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];
        }
    }).result(function(event, data, formatted) {
        //redirect to the URL in the string
    var pieces = formatted.split("::");
        window.location.href = '/profile.php?id='+

<?php
mysql_connect ("***", "***","***")  or die (mysql_error());
mysql_select_db ("***");
$req = mysql_query("select id, Username, EmailAddress from ***");


while($dnn = mysql_fetch_array($req))

{
   echo $dnn['id']; 
 }
?>


;

  console.log(data);
  console.log(formatted);

    });
 });
</script>

1 个答案:

答案 0 :(得分:1)

您的MySQL查询对数据库中的每个用户都是如此,因此它返回所有用户。如果你想转到“foo”的个人资料,你需要告诉数据库只提取“foo”的id。用户拥有的唯一行可能是电子邮件,必须是用户名。

我假设您在javascript中有一个包含所选用户的数组:

var users = new Array("Daniel","Amy","Sandy");

然后你需要使用ajax与php进行通信:

<script>
function ajaxObj( meth, url ) {
    var x = new XMLHttpRequest();
    x.open( meth, url, true );
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}
function ajaxReturn(x){
    if(x.readyState == 4 && x.status == 200){
        return true;    
    }
}//This can become an external file to link
</script>

所以你可以将数据发布到php:

<script>
var returnedStr = "";
function searchuser(){ //use searchuser function on a button to call
    var usersStr = users.toString(); //the string that contain the users separated by ","
    var ajax = ajaxObj("POST", "thisurl.php");
    ajax.onreadystatechange = function() {
    if(ajaxReturn(ajax) == true) {
        if(ajax.responseText == "fail"){ //i didn't include this in php, but you can add it yourself if you can't fetch from mysql
            echo "Failed";
        } else {
    returnedStr = ajax.responseText;// when php echos
        }
    }
}
ajax.send("u="+usersStr);
}
</script>

那么你的php需要处理字符串:

<?php
if(isset($_POST["u"])){
    $returnArr = array();
    $returnStr = "";
    $processedArr = explode(',', $_POST['u']); //Here the posted data will turn into an array
    $lengthArr = count($processedArr);
    for ($i=0; $i<=$lengthArr; $i++)
    {
        $req = mysql_query("SELECT id FROM xxx WHERE Username='$processedArr[$i]' LIMIT 1");
        while($dnn = mysql_fetch_array($req))

        {
            array_push($returnArr, $dnn['id']);
        } 
    }
    $returnStr = implode(",",$returnArr);
    echo ($returnStr);
}
?>

现在Javascript returnedStr希望是1,2,3或类似的东西。 如果这不起作用,请发表评论!