0

我刚刚使用了我的第一个 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>";
}
?>
4

1 回答 1

1

您在“search.php”中的以下代码行中犯​​了错误。还要验证其他代码行。

echo "<a href=anotherpage.php?item=".$id."</a>";
  echo "$name";

上面的代码应该如下所示:

 echo "<a href=anotherpage.php?item=".$id.">".$name."</a>";

于 2013-05-03T12:02:40.160 回答