[SalesForce] How to redirect from one visualforce page to another

I have a simple VF page as attached, upon clicking on the submit button it should take the phone number to another VF page and execute a javascript button on other page.

Here is my code:

<apex:page controller="phone" >
<script type="text/javascript">
function submit() {
window.location = '/apex/FindPage';
}
</script>
    <apex:form >
           <div class='form-group'>
           Enter Phone number here:
               <input class="form-control" type="text" id="phone" placeholder="Phone" />
           </div>
        <apex:commandButton styleClass="btn btn-default" value="Submit" id="findPhone" onclick="submit()"/>    
    </apex:form>
</apex:page>

enter image description here

Best Answer

It depends on how you want to do the redirection.

1.If you want to directly link to the other page, there is the Visualforce $Page global variable.

2.If you want to send data back to the server, or otherwise perform some server based action before performing the redirect, you would want that method in your controller to return a PageReference.

In your case, since it sounds like you are building a wizard, you would want to go with method 2. On your visualforce page, your command button will call a method on the controller to pass back the current state of the page with the input values,

<apex:commandButton action="{!goToFindPage}" value="Submit" />

then the called method in the controller will return a page reference to perform the redirect.

public PageReference goToPageTwo(){
    ⋮
    return new PageReference('/apex/FindPage');
}
Related Topic