n last month’s 10-Minute Solution, you learned how to display data from a database. This time, you’ll see how to use this technique to pick and choose what to display from the database based on the user’s choice. Before you begin, you need to get your database ready. You can access any ODBC-compliant database from your ASP pages using ActiveX Data Objects (ADO). This means you can access a Microsoft Access database, a Microsoft SQL Server database, or even an Oracle database. For the sake of this article, I will demonstrate how to access a Microsoft Access database.
Imagine that you’re presenting a page where the user can choose to see a list of customers. Instead of dumping the entire contents of the table “Customers”, this time you let the user choose the criteria for selecting the customer. Assume that the user wishes to view customers filtered by their state. After the user enters or chooses a state, you display the customers that reside in that state.
When a user accesses a page from a Web site, the browser sends a request to the Web server. The Web server then serves or returns the page. The user can take action on the page and either visit another link, or if it is an HTML form page, submit the form. If the user submits a form, a specific page on the Web server will receive the submitted form’s data.
First, you need a page that displays an HTML form asking users what state they wish to pick. You will need a second page that will accept that information, obtain the data from the database, and then display the data. Figure 1 shows the chain of events that occur when the user first visits the page till the user gets the data requested.
Normally, textbooks will teach that you need two pages on the server to handle this scenario: the page that displays the HTML form, and the page that accepts the form data and returns the data from the database. However, from my experience, it is much easier to code this schema with a single ASP page. The code remains in a single location, making it easier to maintain, debug, and enhance. However, you will need to add code to distinguish the browser’s request?is it asking for page 1 or returning the HTML form’s data? You can create each page portion separately and then see how you can link them up to be part of the same page.
Build the HTML Form
For this demo, you will create a simple HTML form. To begin with, you can make it really simple and provide the user with a label, a text box, and a command button. The user types in a state abbreviation in the text box and clicks the command button to submit the form. Create a non-Active Server Pages (ASP) page called Page1.htm and use this code to generate the HTML form (see Listing 1). This makes it easy to test and debug.
This code should result in an HTML form that looks like Figure 2. Notice that you have three form controls on the page: a text box called “tS”, a command button called “cmd”, and an invisible textbox called “PASS”, which will always contain the value “1”. When the user submits this form, you will be able to read the values of these controls. Also note that you have not yet completed the form tag in your code. For this testing page, you only used