Hi!

In one of my html forms I had to use html element "<select multiple 
name=...", so when the user selects multiple items, we got a datastream 
like this:
var1=value1&var1=value2&var1=value3... etc.
Original function returned only the first value of var1. I know the 
remark states that it retrieves only a single value, but since I did not 
found an other function retrieving multiple values like that, I took 
some guts, and made some modification to it.
Just in case someone faces the same problem, here is the modified code.
In case of multiple values, they are separated by CRLFs, so the result 
can be loaded to a TString descendent's Text property for example .

Peter

function ExtractURLEncodedValue(
   Msg       : PChar;        { URL Encoded stream                     }
   Name      : String;       { Variable name to look for              }
   var Value : String)       { Where to put variable value            }
   : Boolean;                { Found or not found that's the question }
var
   NameLen  : Integer;
   FoundLen : Integer; {tps}
   Ch       : Char;
   P, Q     : PChar;
begin
   Result  := FALSE;
   Value   := '';
   if Msg = nil then         { Empty source }
       Exit;

   NameLen := Length(Name);

   P := Msg;
   while P^ <> #0 do begin
       Q := P;
       while (P^ <> #0) and (P^ <> '=') do
           Inc(P);
       FoundLen := P - Q; {tps}
       if P^ = '=' then
           Inc(P);
       if (StrLIComp(Q, @Name[1], NameLen) = 0) and
          (NameLen = FoundLen) then begin  {tps}

          {*** Multiple Values ***}
          If Value <> ''
            Then Value := Value + #13#10;  // separate values by CrLf
          {**********}

           while (P^ <> #0) and (P^ <> '&') do begin
               Ch := P^;
               if Ch = '%' then begin
                   Ch := chr(htoi2(P + 1));
                   Inc(P, 2);
               end
               else if Ch = '+' then
                   Ch := ' ';
               Value := Value + Ch;
               Inc(P);
           end;
           Result := TRUE;
           // break;       {*** Commented *** We are looking for more 
"Name"-s}
        end;
        while (P^ <> #0) and (P^ <> '&') do
            Inc(P);
       if P^ = '&' then
           Inc(P);
   end;
end;

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to