[SalesForce] How to pass the Value from One VF page to another Page without using URL param

I'm trying to pass the value from a page variable to another page (both using different controller) without using parameters.

This is what am working on that.

I have user search page with input field.

It's a user search,If the user not found in the search, i provide the link below and says that click here to register. The input field search value which i have give should be populated in the Another page.

To achieve this how to pass the value, Please help me on this.

Page 1:

<apex:page controller="SearchPageController" sidebar="false" showHeader="false" docType="html-5.0" cache="false" id="pg">
<style>
    .slds-scope .slds-box {
    padding: 2rem;
    border-radius: 1.25rem;
    border: 4px solid #a4bfe4;
    width: 100%;
    padding-left: 55px;
    padding-right: 55px;
    margin: 0 auto;        
    background-color: #fdfdfd;
    } 
    .searchBox{
    border: 2px solid #a4bfe4;   
    border-radius: 1.25rem;
    padding-left: 5px;
    width: 25%;
    height : 30%;
    }

    .controls{
    background-color:#fdfdfd;
    }
</style>
<apex:form id="fm">

    <script>

    function conRec () {
        //  alert('conRec');
        var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
        var SearchVal = document.getElementById('pg:fm:pb:SearchValue').value;
        alert(SearchVal);
        if(SearchVal.match(pattern)){
            alert('true');
        }else{
            alert('False');
        }
    }
    </script>

    <apex:slds />
    <apex:pageBlock id="pb" >


        <div class="slds-scope slds-box">
            <apex:outputPanel >                   
                <div id="formBlock">

                    <div id="showErrorMsgbuserName">
                        <div class="slds-form-element">
                            <div class="slds-align_absolute-center">
                                <!--- <apex:input value="{!searchBoxstring}"  id="SearchID" styleClass="searchBox"/>  --->

                                <apex:input value="{!searchBoxstring}" styleClass="searchBox" id="SearchValue" />
                                &nbsp;  &nbsp;  &nbsp; <apex:commandButton value="Search" onClick="conRec()" action="{!display}"   >

                                </apex:commandButton>
                                <div class="slds-hide slds-form-element__help" id="error-message-userName">This field is required</div>
                            </div> 
                        </div>
                    </div>

                </div>

                <apex:pageMessages id="Error" escape="false" />


                <br/>
                <br/>    
                <apex:dataTable value="{!result}" var="con" border="1" cellpadding="5" cellspacing="4" bgcolor="lightblue">
                    <apex:column value="{!con.Name}" styleClass="controls">
                        <apex:facet name="header">Name</apex:facet>
                    </apex:column>
                    <apex:column value="{!con.Email}" styleClass="controls">
                        <apex:facet name="header">Email</apex:facet>
                    </apex:column>
                    <apex:column value="{!con.Phone}" styleClass="controls">
                        <apex:facet name="header">Phone</apex:facet>
                    </apex:column>
                </apex:dataTable>                   

            </apex:outputPanel>   
        </div>
    </apex:pageBlock>    


</apex:form>
</apex:page> 

Controller:

public class SearchPageController {

public string searchboxstring{get; set;}
public String Email{get;set;}

public list<contact> result {get; set;}

public SearchPageController()
{
    result = new list<Contact>();
}

public pagereference display(){
    system.debug('searchboxstring1-->' + searchboxstring);
    Email = searchboxstring;        
    result=new List<Contact>();
    system.debug('searchboxstring-->' + searchboxstring);
    Email = searchboxstring;
    system.debug('Email-->' + Email);
    //result=[select ID, Name, Email, Phone  from Contact where FirstName like :searchboxstring or LastName  like :searchboxstring or Email like :searchboxstring or Phone like :searchboxstring];
    result=[select ID, Name, Email, Phone  from Contact where Name like :searchboxstring or Email like :searchboxstring or Phone like :searchboxstring];
    system.debug('result-->' + result.size());
    if (result.size() < 1) {            
        ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'No data Found' + '<a href='+ System.URL.getSalesforceBaseUrl().toExternalForm()+ '/apex/Registration' + '>Click Here</a>' + 'to Register  Or '+ '<a href='+ System.URL.getSalesforceBaseUrl().toExternalForm()+ '/apex/Registration' + '>Click Here</a>' +' to Send Email to Register'));
    }

    return null;

}
}

Best Answer

You can use Apex Cookies to pass values between pages. Here's a working demo.

On PageOne, whatever value user enters in the input box, that value will be stored in greeting cookie and will be retrieved on PageTwo and displayed on the page.

// Page One

<apex:page controller="PageOneCtrl" docType="html-5.0">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection collapsible="false" columns="1">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel>Greeting: </apex:outputLabel>
                    <apex:input type="text" value="{!greeting}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton value="Set Cookie" action="{!setCookieAndNavigate}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page> 

// Page One Controller

public class PageOneCtrl {

    public String greeting {get; set;}

    public PageReference setCookieAndNavigate(){
        Cookie c = new Cookie('greeting', greeting, null, -1, false);
        ApexPages.currentPage().setCookies(new Cookie[]{c});
        PageReference pageRef = Page.PageTwo;
        return pageRef;
    }
}

// Page Two

<apex:page controller="PageTwoCtrl">
    Hello, {!greeting}.
</apex:page>

// Page Two Controller

public class PageTwoCtrl {

    public String getGreeting(){
        Cookie greeting = ApexPages.currentPage().getCookies().get('greeting');
        return greeting.getValue();
    }
}

Hope this helps.

Related Topic