Programming with HTML Forms / Example 2: The File Searcher
Example 2: The File Searcher
Programming with HTML Forms
The input document for the file searcher has the same format as before, but can be found at:
The only change is that its form's ACTION attribute refers to "http://www.cs.mu.oz.au/cgi-bin/qdir".
qdir searches through a text file holding a membership list. It looks for lines containing the strings entered in the text entry fields of the form, and at most 10 matching lines are printed, together with the total number of matching lines.
The query like the one shown in Figure 2 results in the HTML document shown in Figure 4 being generated by the application. It can also produce an error document, as shown in Figure 5, if no strings are entered before a search is initiated.
Query Result Only a maximum of 10 matching lines are shown for any search.
The following strings are being used for the search:
- Bell, John; DoCS; Queen Mary and Westfield College; Mile End Road; London E1 4NS; UK; Tel: 071 975 5210; Email: email@example.com
- Fox, John; ICRF; Advanced Computation Laboratory; PO Box 123,; Lincoln's Inn Fields; London WC2A 3PX; UK; Tel: 071 269 3624; Email: firstname.lastname@example.org
- Johns, Nicky; LPA; Studio 4; Royal Victoria Patriotic Building; Trinity Road; London SW18 3SX; UK; Tel: 081 871 2016; Fax: 081 874 0449; Email: email@example.com
- Jones, John; DoCS; Univ. of Hull; Hull; HU6 7RX; UK; Tel: 0482 465767 or 465951; Email: firstname.lastname@example.org
- Lloyd, John; Dept of CS,; Univ. of Bristol; Queen's Building,; Univ. Walk; Bristol BS8 1TR; UK; Tel: 0272-303913; Email: email@example.com
Figure 4: Reply to input like that in Figure 2
Must specify at least 1 pattern
Figure 5: Error reply if no strings supplied
The application C code (in file qdir.c) can be found at:
The compiled version, qdir, is in /local/dept/wwwd/scripts.
qdir begins in a similar way to qgp by outputting the start of its reply HTML document and then checking for CGI errors.
The call to record_details() is explained in Part 8, 'Logging'. It logs information about the user in a file, and has no effect on the subsequent code.
get_pats() initially calls build_entries() to obtain the name=value pairs in a useable form. The values are then extracted and placed in the patterns array. These values correspond to the search strings entered by the user in the form's text entry fields.
print_pats() prints the search strings as an unnumbered HTML list.
process_pats() uses the search strings to form a UNIX command. The idea is to translate a single search string, such as John, into the command:
fgrep is a UNIX utility for quickly searching through text files.
Multiple search strings, such as John, uk, and LPA, would be utilized in the following command:
The trick is to pipe the matching lines of one fgrep call into another fgrep call, which further filters the selection.
The matching lines are printed by print_matches(), which reads at most 10 lines from the temporary file.
The total number of lines in the temporary file is counted by the wc UNIX command:
The line count is read in via a pipe which captures the output from the command.
This code demonstrates how to utilise UNIX features as part of an application, which is preferable in this case because of the size of the file being searched, and the potentially large number of matching lines that need to be manipulated. These techniques for utilizing UNIX can also be employed to create forms that edit files, send mail, read news, or monitor the network.
Comments are welcome
Copyright 1996 Andrew Davison and
Created: Apr. 26, 1996
Revised: May 7 1996