Nowadays, blogs are very popular. I believe that anyone who needs to spend a little time surfing the internet will have a blog here or there. Some friends with certain abilities may prefer to download a blog program to set up their own blog, rather than using the services provided by some blog websites. Most blog programs use the search function to submit query keywords to the search page, generate search results in the background, and then present the results to users. This process wastes some bandwidth, for example, the sidebar of a blog. To save some bandwidth, we can use ajax to create our own brushless new log search.
In this tutorial, the table name and log view page of the database take L-blog as an example, because my blog program is modified from L-blog.
The example in this tutorial has passed the actual test and can be used directly in L-blog or FBS. Of course, you still need to do some beautification and Improvement for real application.
In the database, the log content data table is named blog_content. The log ID is log_id, the log title is log_title, the log view page is blogview. asp, and the parameter is the log logid. With these materials, you can create an XML document template for search results. When displaying search results, you must display the log title and log ID to create a link to view logs.
Search Result template sample. xml
Code:
<? XML version = "1.0" encoding = "UTF-8"?>
<Blogsearch>
<! -- Each reslut is a search result -->
<Result>
<! -- Log ID -->
<Logid> 1 </logid>
<! -- Log title -->
<Logtitle> Ajax first-time experience </logtitle>
</Result>
</Blogsearch>
Each result is a search result. To handle the situation where no relevant content is found, I define the logid as # When the search result is empty #.
After completing the XML document template, you can use ASP to dynamically generate the XML document required for search results. The search keyword is passed in post mode.
Search result output ajaxsearch. asp
Code:
<! -- # Include file = "commond. asp" -->
<! -- # Include file = "include/function. asp" -->
<%
'Commond. asp is the database connection file
'Function. asp has the function checkstr to be used.
Dim search_word, xml_result, rssearch, sqlsearch
Set rssearch = server. Createobject ("ADODB. recordset ")
'Get the search keyword
Search_word = checkstr (TRIM (request. Form ("searchword ")))
'Xml Document Header
Xml_result = "<? XML version = "" 1.0 "" encoding = "" UTF-8 ""?> <Blogsearch>"
If search_word <> empty then
'Create a query SQL statement
Sqlsearch = "select log_id, log_title, log_content from blog_content where log_title like '%" & search_word & "%' and log_isshow = true order by log_id DESC"
'Open record set
Rssearch. Open sqlsearch, Conn, 1, 1
'If there is no search result, a result is generated. The logid is #, which indicates no search result.
If rssearch. bof and rssearch. EOF then xml_result = xml_result & "<result> <logid >#</logid> <logtitle/> </result>"
'Output the search result cyclically
Do while not rssearch. EOF
'Output every result cyclically
Xml_result = xml_result & "<result> <logid>" & rssearch ("log_id") & "</logid> <logtitle> <! [CDATA ["& rssearch (" log_title ") &"]> </logtitle> </result>"
Rssearch. movenext
Loop
Else
'If the keyword is null, no search result is returned.
Xml_result = xml_result & "<result> <logid >#</logid> <logtitle/> </result>"
End if
Xml_result = xml_result & "</blogsearch>"
'Set MIME type to XML document
Response. contenttype = "application/XML"
'Response. charset = "UTF-8"
'Output search results
Response. Write (xml_result)
%>
With the output of the background search results, you can start to write the front-end search results.
First, you need to enter the search keywords and display the search results. I use Div to display these two parts separately:
Ajaxsearch.htm
Code:
<! -- Javascript is used and external links are imported. -->
<SCRIPT type = "text/JavaScript" src = "ajaxsearch. js"> </SCRIPT>
<! -- User input -->
<Div>
<! -- Because no form is used, the keydown event of input must be processed. Search after the user presses enter -->
<Input type = "text" id = "searchword" onkeydown = "If (event. keycode = 13) ajaxsearch ();"/>
<! -- Search button -->
<Input type = "button" onclick = "ajaxsearch ();" value = "Search"/>
</Div>
<! -- Search result display part -->
<Div id = "search_result">
<! -- Prompt the user to enter a search keyword at the beginning -->
<Ul> <li> enter keywords </LI> </ul>
</Div>