Salesforce Visualforce: html form post method in visualforce page

In certain situations develeopers select to use html form tag <form> in the visualforce pages. When the form tag is used without any method attribute then it is get method by default, due to this behavior whenever the page is submitted the data will be visible in the URL..

Form get method will carry all the form data over the URL where as post method will not. In the same way standard salesforce <apex:form> tag will use post method by default which is why we never see any data in the URL when the page is submitted (other than explicit parameters we use).

Now we will see an example snippet where we try to use html form tag with get method and post method.

GET Method:

VF Page Input
<form action="{!URLFOR(ACTION_PAGE)}" method="GET" target="_top">
    <br/>
    <input type="text" name="test.test:info" id="input" value="Please type some info to pass over POST"/>
    <input type="submit" value="confirm"/>
</form>

VF Page for Output
<form>
    <h1>SFDC - DEMO PROCESS for FORM </h1> 
    <br/>
    <textarea name="receieve" style="width:500px;height=400px;">{!$CurrentPage.parameters.test.test:info}</textarea>
    <br/>
</form>

POST Method:

VF Page Input
<form action="{!URLFOR(ACTION_PAGE)}" method="GET" target="_top">
    <br/>
    <input type="text" name="test.test:info" id="input" value="Please type some info to pass over GET"/>
    <input type="submit" value="confirm"/>
</form>


VF Page for Output
<form>
    <h1>SFDC - DEMO PROCESS for FORM </h1> 
    <br/>
    <textarea name="receieve" style="width:500px;height=400px;">{!$CurrentPage.parameters.test.test:info}</textarea>
    <br/>
</form>


Like this we can use html form tags in case of complex scenarios. We know that {!$CurrentPage.parameters.param} will contain all the URL parameters but it will also hold the post method form data also. we can access form elements with name in the currentpage.paramaters.

I have used element name as test.test:info to show that we can use dot and colons in the name which will not be a problem to access the element.

DEMO
                                                             Click Here

Comments