How to Call Apex Controller Method using JavaScript in Visualforce Page?

Sample Code:


Visualforce Page:

<apex:page controller="sampleController">
  
    <script type="text/javascript">
        function jsfind()
        {
            callfind();
        }
    </script>
   
    <apex:pagemessages />
   
    <apex:form >
    <apex:actionFunction name="callfind" action="{!searchAndfind}" reRender="a" />
    <apex:pageBlock >
        <apex:pageblockSection >
            <apex:outputLabel >Query:</apex:outputLabel>
            <apex:inputText value="{!qry}"/>       
        </apex:pageblockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Query" onclick="jsfind()"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Class: 

public class sampleController{

    public String qry{get;set;}

    public pageReference searchAndfind() {
        pageReference pg = new pageReference('https://www.google.com/search?q='+qry);
        pg.setRedirect(true);
        return pg;
    }      
}

Follow Us

Posted By : Sudeer Kamat Date :

view_module Related

label Labels

Comments 0