0
我有從MySQL搜索自動完成的代碼,匹配查詢可以點擊並指引我到一個新的頁面。我怎樣才能將點擊查詢的結果顯示在新頁面中,無需使用數據庫中的任何URL,因爲我需要避免使用大量的HTML文件。謝謝。PHP Ajax顯示點擊結果
<p id="searchresults">
<?php
\t // PHP5 Implementation - uses MySQLi.
\t // mysqli('localhost', 'yourUserbookTitle', 'yourPassword', 'yourDatabase');
\t $db = new mysqli('localhost', 'root', '', 'book');
\t
\t if(!$db) {
\t \t // Show error if we cannot connect.
\t \t echo 'ERROR: Could not connect to the database.';
\t } else {
\t \t // Is there a posted query string?
\t \t if(isset($_POST['queryString'])) {
\t \t \t $queryString = $db->real_escape_string($_POST['queryString']);
\t \t \t
\t \t \t // Is the string length greater than 0?
\t \t \t if(strlen($queryString) >0) {
\t \t \t \t $query = $db->query("SELECT * FROM bookinfo WHERE bookTitle LIKE '%" . $queryString . "%'");
\t \t \t \t
\t \t \t \t if($query) {
\t \t \t \t \t // While there are results loop through them - fetching an Object.
\t \t \t \t \t
\t \t \t \t \t // Store the category id
\t \t \t \t \t $bookTitle = 0;
\t \t \t \t \t while ($result = $query ->fetch_object()) {
\t \t \t \t \t \t if($result->bookTitle != $bookTitle) { // check if the category changed
\t \t \t \t \t \t \t echo '<span class="category">'.$result->bookTitle.'</span>';
\t \t \t \t \t \t \t $bookTitle = $result->bookTitle;
\t \t \t \t \t \t }
\t \t \t \t echo '<a href="'.$result->url.'">';
\t \t \t \t echo '<img src="search_images/'.$result->bookimage.'" alt="" />';
\t \t \t \t
\t \t \t \t $bookTitle = $result->bookTitle;
\t \t \t \t if(strlen($bookTitle) > 35) {
\t \t \t \t \t $bookTitle = substr($bookTitle, 0, 35) . "...";
\t \t \t \t } \t \t \t \t
\t \t \t \t echo '<span class="searchheading">'.$bookTitle.'</span>';
\t \t \t \t
\t \t \t \t $author = $result->author;
\t \t \t \t if(strlen($author) > 80) {
\t \t \t \t \t $author = substr($author, 0, 80) . "...";
\t \t \t \t }
\t \t \t \t
\t \t \t \t echo '<span>'.$author.'</span></a>';
\t \t \t }
\t \t \t echo '<span class="seperator"><a href="http://www.marcofolio.net/sitemap.html" title="Sitemap">Nothing interesting here? Try the sitemap.</a></span><br class="break" />';
\t \t \t \t } else {
\t \t \t \t \t echo 'ERROR: There was a problem with the query.';
\t \t \t \t }
\t \t \t } else {
\t \t \t \t // Dont do anything.
\t \t \t } // There is a queryString.
\t \t } else {
\t \t \t echo 'There should be no direct access to this script!';
\t \t }
\t }
?>
</p>
請顯示你的代碼。 –
請幫忙。謝謝 –