gpt4 book ai didi

php - 自动完成菜单中的单独网址

转载 作者:行者123 更新时间:2023-11-29 00:26:49 25 4
gpt4 key购买 nike

我有一个自动完成的 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>

最佳答案

您的 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 或类似的东西。如果这不起作用,请发表评论!

关于php - 自动完成菜单中的单独网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18523490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com