4

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; 
}
1
  • any reason you aren't just updated the constructor of the PortalSearch apex page? Commented Apr 8, 2013 at 21:49

2 Answers 2

4

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.

2

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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .