我刚刚使用我的第一个AJAX来搜索MySQL数据库。这是一个人名,我通常会添加一个href到结果(我有)并张贴到一个单独的页面,以显示所有相关的细节,例如地址,等级和位置。但是我想知道这是否可能停留在同一页面上。这就是我到目前为止所拥有的。 (是的,CSS将移动到一个单独的页面)
<?php
if(!strlen(trim($_SESSION['Unique_Id']))) {
header("Location:roster.php");
exit();
}
?>
<title>Search Roster</title>
<style>
body {
font-family: "Lucida Grande", Verdana, Arial, sans-serif;
font-size: 12px;
}
#results {
color: #fff;
background: #414141;
width: 180px;
max-height: 200px;
padding-left: 4px;
border: 1px solid #000;
overflow-y: scroll;
overflow-x: hidden;
}
a { color: #fff; display: block; }
a:hover { background: #666666; }
</style>
Keyword Search:
<form id="searchform" method="post" onsubmit="return false;"><input autocomplete="off" id="searchbox" name="searchq" onkeyup="sendRequest()" type="textbox">
</form><div id="show_results"></div><script src="config/prototype.js" type="text/javascript">
</script>
<script>
function sendRequest() {new Ajax.Updater('show_results', 'pages/login/search.php', { method: 'post', parameters: $('searchform').serialize() });
}
</script>
<?php include("pages/include/footer.php"); ?>
和search.php页面
<?php
$host="****"; // Host name
$sqlusername="****"; // Mysql username
$sqlpassword="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="****"; // Table name
mysql_connect("$host", "$sqlusername", "$sqlpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$searchq = $_POST['searchq'];
if(empty($searchq)) {
echo "";
} else {
echo "<div id='results'-->";
//This query searches the name field for whatever the input is.
$sql = "SELECT * FROM USER WHERE Employee LIKE '%$searchq%' OR staff LIKE '%$searchq%'";
echo "<table border='3'>
<tr>
</tr>";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$name = $row['EmployeeName'];
$id=$row['Unique_Id'];
echo "<a href=anotherpage.php?item=".$id."</a>";
echo "$name";
echo "</br>";
}
echo "</div>";
}
?>
答案 0 :(得分:1)
您在“search.php”中的以下代码行中出错了。还要验证其他代码行。
echo "<a href=anotherpage.php?item=".$id."</a>";
echo "$name";
echo "<a href=anotherpage.php?item=".$id."</a>";
echo "$name";
以上代码应如下所示:
echo "<a href=anotherpage.php?item=".$id.">".$name."</a>";