Obtain All Values from Multivalue Input Fields in WebBroker Applications (cont'd)


Collecting Values from All Input Fields
A colleague and friend of mine, Micha Somers, found it more convenient to walk through the entire list of ContentFields and produce another StringList that contained only the field values for a specified fieldname. The following routine accommodated Micha's preference, and it has an additional benefit: you can call it not only with the ContentFields but also with the QueryFields as argument:
procedure GetStringsValues(AStrings: TStrings;
                           AKey: String; AValueList: 
TStringList); var FieldValue: String; i, match: Integer; begin AKey := AKey + '='; for i:=0 to Pred(AStrings.Count) do begin FieldValue := AStrings.Strings[i]; if Pos(AKey, FieldValue) = 1 then
{ exact match } begin match := Pos('=', FieldValue); if match > 0 then AValueList.Add(Copy(FieldValue, match+1,
Length(FieldValue)-match)) end end end; Listing 4. Collecting All Input Fields
I also could have used the Names property to compare the AKey with Names[i]. Name only contains the key names. And I could not have used Values[i] to obtain the value easily, because Values—as you saw earlier—is indexed with a string (the name), not with an integer.

The Final Solution
The final piece of code requests the "Delphi" values from the ContentFields using the GetStringsValues and then lists all items in the resulting stringlist. The code block between try and finally makes sure that your DelphiValues stringlist is freed when you have no further use for it.

procedure TWebModule1.WebModule1WebActionItem1Action(Sender: 
TObject; Request: TWebRequest; Response: TWebResponse; var
Handled: Boolean); var Str: String; i: Integer; DelphiValues: TStringList; begin Str := ''; DelphiValues := TStringList.Create; try GetStringsValues(Request.ContentFields,'Delphi'
,DelphiValues); for i:=0 to Pred(DelphiValues.Count) do Str := Str + DelphiValues.Strings[i] + '&'; finally DelphiValues.Free end; Response.Content := Copy(Str,1,Length(Str)-1); end; Listing 5. Reporting All Multi-value Input Field Values
This time, you can report all possible values (without fieldname) for the Delphi field:
D1&D5
Of course, the DelphiValues stringlist can be used in other places as well. Also, GetStringsValues is a general purpose stringlist routine that can be used outside WebBroker applications as well to obtain a list of values for a specified fieldname.

Bob Swart (aka Dr.Bob) is a Delphi trainer and consultant who has spoken at the Borland Developer Conferences since 1993. As a freelance technical author, he has written chapters for The Revolutionary Guide to Delphi 2 (WROX), Delphi 4 Unleashed, C++ Builder 4 Unleashed, C++ Builder 5 Developer's Guide, and the upcoming Kylix Developer's Guide and Delphi 6 Developer's Guide (SAMS). He can be reached at drbob@chello.nl.



Previous: Single-Value Input Fields
 
Back to Introduction


Introduction Single-Value Input Fields
WebBroker Technology in Action Collecting Values from All Input Fields


Return to Get Help with Delphi Page

Return to Main Get Help Page
 
With Delphi's WebBroker Technology, you can produce Web server applications and obtain user input with HTML forms. However, sometimes you'll need to obtain the data from multivalue input fields like a multi-select listbox, in which case the Values property returns only the first value and not the rest.




Using a small code routine, extract all values from a multivalue input field in WebBroker applications.


Find Out More
Dr. Bob on Delphi Web Modules

The WebBroker: CGI and ISAPI chapter from Charlie Calver's Delphi 4 Unleashed

TALK BACK
This month's 10-Minute Solution is necessary because the current implementation of Value returns only the first value and a string for the name (and not an index) must be specified to find values. What improvements would you make to Delphi to make this solution obsolete? Let us know in the database development discussion groups!
 





Sponsored Links