Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Detect submit button press on a web form in delphi


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Jul 13, 2017 3:06 PM Last Post By: Antonio Estevez
Cornelia von Sc...

Posts: 7
Registered: 11/4/16
Detect submit button press on a web form in delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 6, 2017 5:11 PM
I am using a TWebbrowser control created dynamically to display a webpage defined in a file on my machine.
the html code for this web page is shown below. I am only showing the html code that defines the form called "ccform", which is the only
form on the page defined by this html code.
.....
</style>
<body>
<form id='ccForm' action='" + url2 + "' method='post'>
<label for='cc'>Credit Card Number:</label>
<input type='text' id='cc_number' pattern='
d*' title='Numbers Only' value='' name='cc_number' maxlength='16' required>
<label for='xmonth'>Expiration Date:</label>
<input type='month' id='cc_exp' name='cc_exp' required>

<input type='submit' id='submit' value='Submit'>
</form>
</body>
</html>

These are my variable definitions:
var
WebBrowserTest: TWebBrowser;
document: IHTMLDocument2;
firstForm: IHTMLFormElement;
szFormURL: string;
HTTP: TIdHTTP;

This is my code to display the webpage
try
WebBrowserTest := TWebBrowser.Create(Self);
TWinControl(WebBrowserTest).Parent := Self;
if assigned(WebbrowserTest) then
WebBrowserTest.Navigate('C:\AdagioSource\d10\AR\source\GetCardScreen.html') // the web page is defined in this html file
else
Exit;
Application.ProcessMessages;
document := WebBrowserTest.Document as IHTMLDocument2;
firstForm := GetFormByNumber(document,0);
if Assigned(firstForm) then
begin // set the action for form "ccForm"
FirstForm.action := szFormURL; // szFormURL is a URL returned to me as result from a previous Xml call
end
else
Exit;
finally
end;

I am successfully showing the html page inside my TWebBrowser control but I do not know how to detect a button click in the webpage.
Here's what I need to know:
1) Since I created the TWebBrowser component dynamically, I need to free it in the finally clause. The problem is that I don't know how to detect that the submit button has been clicked in the web page
I want to disable the parent form while the webpage is open, and when the submit button is clicked I want to close or free the TWebBrowser component and proceed to step 2)

2) When the submit button is clicked, the web page goes to the URL (szFormURL) defined as the action, which supposedly returns a string result
I am trying to obtain the response from this URL as follows:

try
CoInitialize(nil);
HTTP := TIdHTTP.Create;
// HTTP GET request
szResponse := HTTP.Get(szFormUrl);
Showmessage(szResponse);
HTTP.Destroy;
finally
CoUninitialize;
end;

This second step is also not working, possibly because it is executed before the submit button has been pressed
I really appreciate any help you can provide. Communication/integration of delphi with webpages is completely new to me, so I am really struggling with this
Thanks
Cornelia

Dan Barclay

Posts: 889
Registered: 11/9/03
Re: Detect submit button press on a web form in delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 6, 2017 6:15 PM   in response to: Cornelia von Sc... in response to: Cornelia von Sc...
Cornelia von Schellwitz wrote:
I am using a TWebbrowser control created dynamically to display a webpage defined in a file on my machine.
the html code for this web page is shown below. I am only showing the html code that defines the form called "ccform", which is the only
form on the page defined by this html code.
.....
</style>
<body>
<form id='ccForm' action='" + url2 + "' method='post'>
<label for='cc'>Credit Card Number:</label>
<input type='text' id='cc_number' pattern='
d*' title='Numbers Only' value='' name='cc_number' maxlength='16' required>
<label for='xmonth'>Expiration Date:</label>
<input type='month' id='cc_exp' name='cc_exp' required>

<input type='submit' id='submit' value='Submit'>
</form>
</body>
</html>

These are my variable definitions:
var
WebBrowserTest: TWebBrowser;
document: IHTMLDocument2;
firstForm: IHTMLFormElement;
szFormURL: string;
HTTP: TIdHTTP;

This is my code to display the webpage
try
WebBrowserTest := TWebBrowser.Create(Self);
TWinControl(WebBrowserTest).Parent := Self;
if assigned(WebbrowserTest) then
WebBrowserTest.Navigate('C:\AdagioSource\d10\AR\source\GetCardScreen.html') // the web page is defined in this html file
else
Exit;
Application.ProcessMessages;
document := WebBrowserTest.Document as IHTMLDocument2;
firstForm := GetFormByNumber(document,0);
if Assigned(firstForm) then
begin // set the action for form "ccForm"
FirstForm.action := szFormURL; // szFormURL is a URL returned to me as result from a previous Xml call
end
else
Exit;
finally
end;

I am successfully showing the html page inside my TWebBrowser control but I do not know how to detect a button click in the webpage.
Here's what I need to know:
1) Since I created the TWebBrowser component dynamically, I need to free it in the finally clause. The problem is that I don't know how to detect that the submit button has been clicked in the web page
I want to disable the parent form while the webpage is open, and when the submit button is clicked I want to close or free the TWebBrowser component and proceed to step 2)

2) When the submit button is clicked, the web page goes to the URL (szFormURL) defined as the action, which supposedly returns a string result
I am trying to obtain the response from this URL as follows:

try
CoInitialize(nil);
HTTP := TIdHTTP.Create;
// HTTP GET request
szResponse := HTTP.Get(szFormUrl);
Showmessage(szResponse);
HTTP.Destroy;
finally
CoUninitialize;
end;

This second step is also not working, possibly because it is executed before the submit button has been pressed
I really appreciate any help you can provide. Communication/integration of delphi with webpages is completely new to me, so I am really struggling with this
Thanks
Cornelia


Hopefully someone else can respond with more specific info but maybe I can give you some info on where to start. It's been some time since I used TWebBrowser, but there are events associated with it. You should be able to assign a Delphi procedure to one of the events and inspect the Submitted data. If I get time in the next few days I'll try to track down the specifics.

Dan
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Detect submit button press on a web form in delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2017 2:48 PM   in response to: Cornelia von Sc... in response to: Cornelia von Sc...
El 07/07/2017 a las 2:11, Cornelia von Schellwitz escribió:
2) When the submit button is clicked, the web page goes to the URL (szFormURL) defined as the action, which supposedly returns a string result
I am trying to obtain the response from this URL as follows:

try
CoInitialize(nil);
HTTP := TIdHTTP.Create;
// HTTP GET request
szResponse := HTTP.Get(szFormUrl);
Showmessage(szResponse);
HTTP.Destroy;
finally
CoUninitialize;
end;

This second step is also not working, possibly because it is executed before the submit button has been pressed
I really appreciate any help you can provide. Communication/integration of delphi with webpages is completely new to me, so I am really struggling with this
Thanks
Cornelia


You don't need a webbrowser to submit a form to the server. The HTTP client component can do all the work.

Just put the following components on the form:
- a TEdit for the credit card number (ex. TEditNumber)
- a TEdit for the expiration date (ex. TEditExpDate)
- a TButton for submit the data to the server (ex. ButtonSubmit).

Add an OnClick event to the button:
procedure TForm1.ButtonSubmitClick(Sender: TObject);
var
   Number: String;
   ExpDate: String;
   Response: String;
   Params: TStringList;
   HTTP: TIdHTTP;
begin
   Number := Trim(EditNumber.Text);
   ExpDate := Trim(EditExpDate.Text);
 
   if (Length(Number)> 0) and (Length(ExpDate)>0) then
   begin
     Params := TStringList.Create;
     try
        Params.Values['cc_number'] := Number;
        Params.Values['cc_exp'] := ExpDate;
 
        HTTP := TIdHTTP.Create;
        try
          Response := HTTP.Post(szFormUrl, Params);
          // use Response as needed
 
        finally
           HTTP.Free;
        end;
     finally
        Params.Free;
     end;
   end;
end;
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02