How do I submit a form with style="display:none"?

Post Reply
zhengyc653
Posts: 3
Joined: Tue Jun 08, 2021 11:41 am

How do I submit a form with style="display:none"?

Post by zhengyc653 »

If the form setting style="display:none", the element values in the form cannot be submitted, but they can be submitted normally on the browser. How to make CEF4 can submit such form normally?
HTML code:

<!DOCTYPE html>
<html>
<body>
<form action="/demo/demo_form.asp" target="_blank" style="display:none">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input id="sb" type="submit" value="Submit">
</form>

<button type="button" onclick="document.getElementById('sb').click()">Click Me!</button>


</body>
</html>
zhengyc653
Posts: 3
Joined: Tue Jun 08, 2021 11:41 am

Re: How do I submit a form with style="display:none"?

Post by zhengyc653 »

Sorry, I took a closer look and found that the targeturl in the onBeforePopup event has no form content. Style ="display:none";
User avatar
salvadordf
Posts: 2966
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How do I submit a form with style="display:none"?

Post by salvadordf »

Perhaps the URLRequest demo can be helpful in this case. That demo shows how to send GET and POST requests.
zhengyc653
Posts: 3
Joined: Tue Jun 08, 2021 11:41 am

Re: How do I submit a form with style="display:none"?

Post by zhengyc653 »

Thank you very much for your answer, but this may have nothing to do with the URLRequest Demo.
HTML:

Code: Select all

<form action="/demo/demo_form.asp" method="get" target="_blank">
	First name:<br>
	<input type="text" name="firstname" value="Mickey">
	<br>
	Last name:<br>
	<input type="text" name="lastname" value="Mouse">
	<br><br>
	<input id="sb" type="submit" value="Submit">
</form> 
Delphi Code:

Code: Select all

procedure TfrmBrowser.Chromium_OnBeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
  targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; const popupFeatures: TCefPopupFeatures;
  var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var extra_info: ICefDictionaryValue;
  var noJavascriptAccess, Result: Boolean);
begin
  Result := true;
  if Self.Showing then begin
    PopupTaretUrl := targetUrl;  // targetUrl = 'demo/demo_form.asp'
    // The expected targetURL value should be:  'demo/demo_form.asp?firstname=Mickey&lastname=Mouse'
    PostMessage(Handle, CEFBROWSER_BEFOREPOPUP_NEWTAB, 0, 0);
  end;
end;
The problem has nothing to do with "display:none", target has an effect on it..

After clicking the Submit button:

If target="_self", the normal jump to:
/demo/demo_form.asp? Firstname = Mickey&lastname = Mouse

If target="_blank", then the targetURL in the onBeforePopup event becomes:
/demo/demo_form.asp
This way, when I open the page in a new TAB, I lose the form content.

I'm so sorry,I don't know English, is to use the translation software translation, the expression may not be very good, hope you can test carefully, thank you.
Last edited by zhengyc653 on Thu Jun 10, 2021 2:19 am, edited 8 times in total.
User avatar
salvadordf
Posts: 2966
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How do I submit a form with style="display:none"?

Post by salvadordf »

I'm sorry I misunderstood.

You need to open the new browser TAB in a similar way that the PopupBrowser2 or the TabbedBrowser2 demos open new popup windows.

Read the code comments in the PopupBrowser2 demo for all the details about popup windows. Then you will have to do something similar with new tabs :
  • Create a hidden tab with an uninitialized browser.
  • Call CreateClientHandler in the TChromium.OnBeforePopup event.
  • Send a message to the main form to show the previous hidden tab and to create a new hidden tab in the main application thread.
User avatar
salvadordf
Posts: 2966
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How do I submit a form with style="display:none"?

Post by salvadordf »

I forgot to clarify one thing.

The POST information is lost when you open new popup windows or new tabs unless your application uses the previous method with the CreateClientHandler call.

Your example uses GET but I would try this method anyway.
zhengyc653
Posts: 3
Joined: Tue Jun 08, 2021 11:41 am

Re: How do I submit a form with style="display:none"?

Post by zhengyc653 »

salvadordf wrote: Fri Jun 11, 2021 4:00 pm I'm sorry I misunderstood.

You need to open the new browser TAB in a similar way that the PopupBrowser2 or the TabbedBrowser2 demos open new popup windows.

Read the code comments in the PopupBrowser2 demo for all the details about popup windows. Then you will have to do something similar with new tabs :
  • Create a hidden tab with an uninitialized browser.
  • Call CreateClientHandler in the TChromium.OnBeforePopup event.
  • Send a message to the main form to show the previous hidden tab and to create a new hidden tab in the main application thread.
In my HTML, PopupBrowser2 and TabbedBrowser2 demos don't pop up and go to the URL they specify.
Could you please make a DEMO with my HTML code? Thank you very much!
User avatar
salvadordf
Posts: 2966
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How do I submit a form with style="display:none"?

Post by salvadordf »

Please, download CEF4Delphi again from GitHub.

The new version includes an improved TabbedBrowser2 demo which can open new tabs and popup windows without losing the POST data. Try it with your web page.
Post Reply