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


Single-Value Input Fields
If you click on the Submit button, the WebProj1 Web server application executes by default. This means the OnAction event handler of our default action item fires. Inside this event handler, you can produce any HTML in the Response.Content string. If you want to reproduce the input that the user has given (e.g., the name "Bob Swart" and the Delphi Versions "Delphi 1" and "Delphi 5"), then you can use ContentFields (if you used the POST protocol) or QueryFields (if you used the GET protocol).

In this case, you have used the POST protocol, so you can use the ContentFields property of the Request object. This property is the TStringList type, which means you can use the Values property to search for the value of specific strings (such as "Name" or "Delphi") and get their values:

procedure TWebModule1.WebModule1WebActionItem1Action(Sender: 
TObject; Request: TWebRequest; Response: TWebResponse; var Handled:
Boolean); begin Response.Content := 'Name = ' + Request.ContentFields.Values['Name'] + '<P>' + 'Delphi = ' + Request.ContentFields.Values['Delphi']; end; Listing 2. Reporting single-value input fields
As you can see from the listing above, I use the Request.ContentFields to search for the values of Name and Delphi and then put them in the Response.Content field. Unfortunately, it returns only one value (the first) of the Delphi field. Only "D1" is returned, and not "D5":
Name=Bob Swart
Delphi=D1
The property values of a TStrings or TStringList object returns only the first match for Delphi and never even indicates that more matches exist.

Multi-Value Input Fields
So, let's now concentrate solely on the multi-valued "Delphi" field. Is the second value even present in the Request object? Well, the ContentFields can be accessed through their Strings properties as well as the Values property. These are of the form "Name=Value", like "Delphi=D1" and Delphi=D5". Knowing that, it's easy to loop through the ContentFields and print the items that start with Delphi=:

procedure TWebModule1.WebModule1WebActionItem1Action(Sender: 
TObject; Request: TWebRequest; Response: TWebResponse; var Handled:
Boolean); var Str: String; i: Integer; begin Str := ''; for i:=0 to Pred(Request.ContentFields.Count) do if Pos('Delphi=',Request.ContentFields.Strings[i]) = 1
then Str := Str + Request.ContentFields.Strings[i] + '&'; Response.Content := Copy(Str,1,Length(Str)-1) end; Listing 3. Reporting Multi-Value Input Fields
This time, you can report all possible values for the Delphi field (including fieldnames):
Delphi=D1&Delphi=D5
Although you have now found a way to obtain all values for the Delphi name, I want to show a solution that is more flexible (and that can also be used outside the WebBroker domain).




Previous: WebBroker Technology in Action
 
Next: Collecting Values from All Input Fields


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