Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

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! :)

share|improve this answer
    
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. –  Pramod Kumar Feb 11 '13 at 13:21
    
You can get around this limitation on inputFile by wrapping components that use actionSupport and rerender with actionRegion. –  crop1645 Aug 8 '13 at 1:38

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>
share|improve this answer
    
I am not sure how would it call "saveApplication" action function when i will click on "Save & Continue" btn. –  Pramod Kumar Feb 11 '13 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')). –  Bachovski Feb 15 '13 at 8:24

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.