Tell me more ×
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 a custom search page for use in Customer Portal.

VF Page:

  <form name="frmSearch">
     <input type="text" name="keyword" id="keyword" value="{!portalSearchModel.searchTerm}" onkeypress="return noenter(event);" style="width:25%; height:25px;margin:0; padding: 0px 6px 0px;" placeholder="What are you looking for?" />
     <input type="button" id="btnSearch" name="btnSearch" value="Search" onclick="searchTerm()" class="go-we3" />
  </form>

 <script type='text/javascript'>  
    function searchTerm(){
        var searchTerm = document.getElementById("keyword").value; 
        var url="/apex/PortalSearch?s="+searchTerm; 
        window.location = url; 
        return false;
    }         
</script>

Is there a way to call this apex method in Controller on button click along with the JavaScript?

    public void logSearchTerm(){
        portalSearchModel.searches();
        Search_Log__c sl = new Search_Log__c(); 
        sl.search_term__c = portalSearchModel.searchTerm;
        sl.user__c = UserInfo.getUserId();     
        insert sl; 
}
share|improve this question
 
any reason you aren't just updated the constructor of the PortalSearch apex page? –  Ralph Apr 8 at 21:49

2 Answers

One way to do it is wrapping the controller method using an actionFunction, which can then be invoked from JavaScript.

<apex:form>
....    
<apex:actionFunction name="logSearchTermAF" action="{!logSearchTerm}" />
</apex:form>

function searchTerm(){
....
logSearchTermAF();
}

JavaScript Remoting is the other alternative.

share|improve this answer

Unless you have a specific reason for using javascript (i.e. doing front validation) you can do this a lot simpler by doing direct bindings.

Example Controller

public class SearchController {

  public String searchTerm { get; set; }

  public void doSearch() {
    logSearchTerm();
    // do search
  }

}

Example Page

<apex:page>
  <apex:form>
    <apex:inputText value="{!searchTerm}"/>
    <apex:commandButton value="Search" action="{!doSearch}"/>
  </apex:form>
</apex:page>
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.