3

I have 2 <apex:form> tag on Visualforce page. like below -

<apex:form id="form1">
-
-
-  Some input fields are here
-

</apex:form>

<apex:form id="form2">
    <apex:commandButton title="Save & Continue" value="Save & Continue" action="      
    {!saveApplication}" />
</apex:form>

Now my requirement is when i click on this form2's "Save & Continue"button then form1 should be submitted and controller's saveApplication function should be called. I know in Java i can write a java script function and can call it on "Save & Continue" button's onClick event. like this -

function submitForms()
{
    document.form1.action = saveApplication.do;   
    document.form1.submit();
}

but how can i do it in salesforce?

Thanks in advance

3 Answers 3

5

Background: I am not sure if there is some other reason for wanting two form tags in your page your not describing in your question. However in Visualforce this is not required, in fact in the past it has caused performance issues with multiple viewstate being output to the page (though this has now been resolved). So I personally would avoid it unless you have good reason to use multiple form tags.

Answer: Basically you can have as many controller functions bound to as many commandButton's as you wish. Visualforce only requires that you have at least one apex:form around them all and it will handle invoking the correct controller function/action method for you. Also unless there is something in your requirements missing from your question, there is also no need for JavaScript to accomplish this.

A section of a Visualforce page that allows users to enter input and then submit it with an or . The body of the form determines the data that is displayed and the way it is processed. It's a best practice to verify that pages and custom components use at most one apex:form tag.

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_form.htm

Hope this helps! :)

2
  • Actually in my requirement i have to upload documents and for that i am using inputFile tag in records and there an inputFile limitation that you can't use it in a block which is getting refreshed or in block which has onComplete attribute. so i created 2 form in setting form i am putting that imputFile code and in first form i am putting all refreshing stuff. Feb 11, 2013 at 13:21
  • You can get around this limitation on inputFile by wrapping components that use actionSupport and rerender with actionRegion.
    – cropredy
    Aug 8, 2013 at 1:38
2

In Visualforce you can do it like in a normal html page, that is using javascript function. But the "problem" in visualforce is that element ids are generated "on-the-fly" so you can not acces your form element just like this document.form1.submit. I would do it with jQuery because it has very powerfull selectors:

<apex:form id="form1">
    ...
</apex:form>

<apex:form id="form2">
    <apex:commandButton value="Save & Continue" action="{!saveApplication}" id="saveButton" />
</apex:form>

<script>
jQuery('[id$=saveButton]').click(function(){
    jQuery('[id$=form1]').submit();
});
</script>
3
  • I am not sure how would it call "saveApplication" action function when i will click on "Save & Continue" btn. Feb 11, 2013 at 13:14
  • You don't need to use JQuery. Why would you load the whole library just for 1 line of code? It can be done simply with Javascript, using: document.getElementById('{!$Component.form1}'). Alternatively, if you put an ID on all your apex tags, in this case <page id="page"><form id="form1">... then the id of the form will inherit the parent id and the final ID will be page:form1 (document.getElementById('page:form1')). Feb 15, 2013 at 8:24
  • This Jquery script helped me - thank you for this!
    – RC82
    Jul 25, 2016 at 18:54
-2

Add a unique styleClass to the form then use jquery to to select the form based on the class name. Finally, get the id.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .