[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Submit form using TWebBrowser component

Posted on 2006-04-14
5
Medium Priority
?
368 Views
Last Modified: 2010-04-05
I am trying to submit a form using the WebBrowser component but cannot figre out how to do this.  There are several fields I need to populate prior to submitting.  Does anyone have any working examples I could look at?  
0
Comment
Question by:jennifere
3 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 16457382
Hi,

Please, look at the html source of the page you want to submit and tell me whether
- there is a form with NAME attribute;
- all fields are of type TEXT, they are a part of the form, have NAME attribute and the names are different;

If yes to the above it's easy, otherwise it's also possible but much more complicated.

Regards, Geo
0
 
LVL 6

Expert Comment

by:saravananvg
ID: 16467345
Hello Sir,

  Check the following site if it of any use to you.

http://www.swissdelphicenter.ch/torry/showcode.php?id=1645

with regards,
padmaja.
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 16470168
Try something like this approach. I built this to be able to submit forms in IE based on the form fields.
I have yet to finish it but it is a start. You can modify to use TWebbrowser but it is not really needed.



function FormSubmit
  ( AFormAction:           String
  ; AFormTitle:            String
  ; AFields:               Array of String
  ; AFieldValues:          Array of String
  ) : Boolean;
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Form: IHTMLFormElement;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
  IEFieldsCounter: Integer;
  btnElement: Integer;
begin
  ShellWindow := CoShellWindows.Create;
  // get the running instance of Internet Explorer
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    // QueryInterface determines if an interface can be used with an object
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      btnElement := -1;
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        Document := WB.Document;
        // if correct instance...
        if Pos(AFormTitle, Document.Title) <> 0 then
        begin
          // count forms on document and iterate through its forms
          IEFieldsCounter := 0;
          for m := 0 to Document.forms.Length - 1 do
          begin
            Form := IDispatch(Document.Forms.Item(m)) as IHTMLFormElement;
            if Pos(UpperCase(AFormAction), UpperCase(Form.action)) > 0 then
            begin
              ovElements := Document.forms.Item(m).elements;

              // iterate through elements
              for i := 0 to ovElements.Length -1 do
              begin
                try
                // if input fields found, try to fill them out
                  if (UpperCase(ovElements.item(i).type) = 'SUBMIT') or
                     (UpperCase(ovElements.item(i).type) = 'IMAGE') then
                  begin
                      btnElement := i;
                  end;
                  if not ( (UpperCase(ovElements.item(i).type) = 'CHECKBOX') or
                           (UpperCase(ovElements.item(i).type) = 'RADIO') ) then
                  begin
                    if (LowerCase(ovElements.item(i).name) = LowerCase(AFields[i])) then
                    begin
                        ovElements.item(i).Value := AFieldValues[i];
                        inc(IEFieldsCounter);
                    end;
                  end
                  else
                  begin
                    if StrToInt(AFieldValues[i]) = 1 then
                    begin
                      ovElements.item(i).Checked := True;
                    end
                    else
                    begin
                      ovElements.item(i).Checked := False;
                    end;
                    inc(IEFieldsCounter);
                  end;
                except
                  // failed...
                end;
              end; { for i...}
              if btnElement > -1 then
              begin
                ovElements.item(btnElement).click;
              end
              else
              begin
                Document.forms.Item(m).submit;
              end;
            end;
          end;  { for m }
        end;  {if Pos(AFormTitle, ...}
      end;  { idoc <> nil }
    end; { wb <> nil }
  end;  { for k }

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FormSubmit('GetOptions.aspx', {FormAction}
             'Options Page', {FormTitle}
             ['__VIEWSTATE', 'OptionsList', 'OptionsList', 'OptionsList'] {AFields},
             ['', '0', '0', '1'] {AFieldValues}
             );
end;
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question