Mega Code Archive

 
Categories / Delphi / Examples
 

Making an active web-page, part 2

This time we're going to make a Webpage that's coupled to a databasetable, and we're going to send SQL-enquiries from our Web-interface. You can use any database you like for this purpose. In this example we're going to use standard Borland db-components, so the database must be coupled through the BDE. (You can of course use other components.) We're going to use a table called "CDs", which is a tracklist of CD-recordings. The fieldnames are: Artist, Album, Track, Name, Time. Here's my table: Create a table called "CDs" with these fieldnames in your database, and fill it with some data. Well let's get into business ! Start Delphi and select File / New / Web Server Application. Select :CGI stand-alone executable and Delphi presents a WebModule for us to work with. Select: Project / Options / Application and write "cgi" into Target file extension. Select: Project / Options / Application / Directories/Conditionals and write your servers cgi-bin library into Output Directory Save the project as "test2.dpr" Here's the HTML-code for the search-header: <HTML> <HEAD> <TITLE>CDs</TITLE> </HEAD> <BODY> <FORM ACTION="/cgi-bin/test2.cgi/search" METHOD="POST"> <FORM> <P><FONT FACE="Arial"><B><FONT SIZE="+2" COLOR="#0000FF">My CD:s</FONT></B></FONT></P> <P><FONT FACE="Arial" SIZE="-1">Search for...</FONT></P> <P><FONT FACE="Arial" SIZE="-1"></FONT></P> <TABLE WIDTH="50%"> <TR> <TD WIDTH="94"><FONT FACE="Arial"><B>Artist:</B></FONT></TD> <TD WIDTH="265"> <P><INPUT TYPE="TEXT" NAME="Artist"></P> </TD> </TR> <TR> <TD WIDTH="94"><FONT FACE="Arial"><B>Volym:</B></FONT></TD> <TD WIDTH="265"> <P><INPUT TYPE="TEXT" NAME="Album"></P> </TD> </TR> <TR> <TD WIDTH="94"><B><FONT FACE="Arial">Titel:</FONT></B></TD> <TD WIDTH="265"> <P><INPUT TYPE="TEXT" NAME="Title"></P> </TD> </TR> </TABLE> <P><INPUT TYPE="SUBMIT" NAME="Submit1"></P> </FORM><FONT FACE="Arial" SIZE="-2"><BR> This code will result in a page like this: For displaying the dbtable we'll need a TDataSetTableProducer. Place one, (from the Internet-group), on the WebModule. We also need a TQuery-component for making queries to the DB. Put one on your WebModule. Put the name of your database into the Query1.DatabaseName - property. Put "select * from CDs" into the Query1.SQL - property and set Query1.Active to true. Paste the HTML-code above into the DataSetTableProducer.Header - property Right-click on the DataSetTableProducer - component and select: Response Editor, and if everything is right you would see this: Not Bad, eh... Well let's say we want a sorted output, change the Query1.SQL - property to: "select * from CDs order by Artist, Album, Track". Presenting this output is the first action we want the dispatcher to take. To make this happen: Right-click on the DataSetTableProducer - component and select: Action Editor Select Add New, set Default to true. Select Events and double-click in the OnAction - event. Put this code into the default action: Response.Content:= DataSetTableProducer1.Content; Compile. It's time to test. We're going to call the dispatcher's default action. To do this, put "http://<your servername>/cgi-bin/test2.cgi" into your WebBrowser. You should get an output similar to the one you got from the Response Editor. Not much coding so far. Now it's time to make the search-action: Right-click on the DataSetTableProducer - component and select: Action Editor Select Add New, and put: "/search" into the PathInfo - property. Select Events, and double-click in the OnAction - event. This will bring us into the code. To get hold of the input parameters for the search, we will look at the Request - object, just as we did in the Part 1 example. Then we have to put these inputs into a SQL-query , and let the DataSetTableProducer reflect the query-output. Last we will send HTML-code to our WebBrowser. Lets start with the code: procedure TWebModule1.WebModule1WebActionItem2Action(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); var Artist, Album, Title: string; begin // Extract the input-parameters from Request with Request.ContentFields do begin Artist:= Values['Artist']; Album:= Values['Album']; Title:= Values['Title']; end; // Put them into a SQL-statement: // "select * from CDs where Artist like '%Artist%' and Album like '%Album%' and // Title like '%Title%' order by Artist, Album, Track" Query1.SQL.Clear; Query1.SQL.Add('select * from CDs where Artist like ' + Chr(39) + '%' + Artist + '%' + Chr(39) + ' and Album like ' + Chr(39) + '%' + Album + '%' + Chr(39) + ' and Title like ' + Chr(39) + '%' + Title + '%' + Chr(39) + ' order by ' + 'Artist, Album, Track'); // Make query Query1.Close; Query1.Open; // Send the response Response.Content:= DataSetTableProducer1.Content; end; Compile the code. Put "http://<your servername>/cgi-bin/test2.cgi" into your WebBrowser, submit your search-entries, and the thing should be working !