Programming with HTML Forms / Example 2: The File Searcher | WebReference

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 "".

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:

The lines found are:

5 line(s) printed.

Figure 4: Reply to input like that in Figure 2

Query Result

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