Coding Search Pages with HTML and ASP
There are many tutorials online that explain the two parts to an ASP-powered search engine for your page. ASP Tutorial is a good example, with separation between the HTML and ASP files needed.
Why Do You Need Two Files?
It's important to understand the difference between the files. ASP runs behind the scenes to perform the search function. HTML is used to both gather the information and then present it to the user.
First, create an HTML file in your favorite WYSIWYG or text editor named something like "ASP_search.html". This is going to be a form that takes in user input, so put it somewhere in the <BODY> of the document:
<form method="POST" action="/cgi-bin/sitesearch.asp"> <table border=0 cellpadding=5 bgcolor=000000><tr> <td align=center> What terms are you searching for? <input type=text name=term size=20> <input type=submit value="search"></td> </tr></table> </form>
Anyone familiar with basic HTML will see some familiar parts of this code that have nothing to do with ASP. Inside of the <FORM> tags is a <TABLE>, with cell padding and background color variables that can be tailored to the designer's preference. The text right before the <INPUT> is also entirely customizable, along with the "size" property of the input field.
The parts of the code that are not variable are the form method and action, which call the Active Server Pages file "sitesearch.asp". The input type also needs to be "submit", although the value on the button can be changed to "search now!" or something like that.
Creating the ASP File
On the server, relative to the ASP_search.html file there needs to be a folder called "cgi-bin". Usually this is a part of any standard server setup already. In that folder you will create the "sitesearch.asp" file that the HTML form calls when the user submits their terms.
The entire code is beyond the scope of this article, but they tend to follow the model as laid out on sites such as ASP Tutorial. There are a few elements of ASP search coding that you need to be aware of in order to customize the search function for your site.
- "Novalid" - in the first few lines of the ASP script you can define what search terms are not acceptable for your site. The sample code gives you some examples to start with:
Novalid=" a b c d e f g h i j k l m n o p q r s t u v w x y z yes no and more"
These are with the idea that single-letter searches or words like "and" are too common to be useful in a search query. Often profanity and inappropriate language is included in the "novalid" list as well.
- "NovalidResponse" - You also get to define what the response alert will be if they try to use a term you don't like:
NovalidResponse ="We can't search for that term. Please ask about something else."
- "FolderName" is where the files are located that are searched. They need to be HTML files, and this sample ASP script will not dig deeper into subdirectories.
- "Search results" is the text which will be shown in the top of the search results page.
Using ASP to Write HTML for Results
The last part of the ASP code is used to write an entirely new HTML file using the "ResponseWrite" ASP function. This is an ephemeral HTML file that is created and shown only in response to one particular search. You can change the presentation of the results by editing the various HTML properties such as "BORDER" and "BGCOLOR", as well as the response if no results are found. However, it is important to keep all of the basic open and closing tags of a standard HTML page. Leaving out even one simple ">" symbol can make the search fail completely.
A Simple ASP Beginning
A tutorial on creating a simple search page is just the beginning of learning Active Server Pages. Other sites such as the W3C Schools and ASP.net (the official Microsoft ASP site) can take you from these basics through the creation of more robust search engines and even developing entire web sites using ASP. Regardless of whether you use it or not, understanding the basics of ASP search pages can help make you a more well-rounded web developer.