+ Start a Discussion
Shiva RajendranShiva Rajendran 

Facing strange behaviour on passing parameter to apex using actionFunction through javascript

The js code that call the apex function is 
function validate()
{
    //  alert("here");
     username=j$('[id$=email]').val();
    
        alert("here ");

    password=j$('[id$=pass]').val();
    // alert(username + "  " +password);
    
 if((username=='') || (password==''))
     {
      document.getElementById("hello").innerHTML="username or password is null";
      return false;   
         }
             
  
   var result= apiToolKitToGetAllCustUser();
    if(result==1)
        {
  alert("in result 1");
            pageRedirectAF(2); // it is the vf actionFunction
             
         }   
            return false;
    }

The vf page code is as follows
 
<button type="submit" class="btn btn-default" onclick="return validate();">login</button>
      <apex:actionFunction action="{!pageRedirectFn}" name="pageRedirectAF">
            <apex:param name="pageData" id="pageData" value="3" assignTo="{!pageData}"/>

     </apex:actionFunction>

Apex Code is as follows :
 
public class custLogin_CC {
    
    public String username{get;set;}
    public String password{get;set;}
    List<CustomUser__c> allUsers{get;set;}
    public Integer pageData{get;set;}
    
    public custLogin_CC()
    {
        
        allUsers=[select Mid__c,password__c,userEmail__c,userName__c from CustomUser__c];
        //pageData=2; if i uncomment this ,it works strangely
        
        
    }
    
    public PageReference pageRedirectFn()
    {
                PageReference pg=null;
           String s= system.CurrentPageReference().getParameters().get('pageData');
                System.debug('pageredirect function called '+ pageData +'   ss ' + s);

        if(pageData==1)
                        pg=new PageReference('/apex/IF_AllQuestionPage_Vf');
        else
                                    pg=new PageReference('/apex/IF_createQuestion_Vf');

        return pg;
        

            
    }
}

Inside constructor in apex class , at line number 12 , if i uncomment the code , the pageData is always 2 at  the pageRedirectFn function. If i comment that line ,then things are working fine.IThe value i pass through the js is auto assigned to the pageData. I want to know if pageData(ie the param variable in vf) shouldn't be initialised if used in param?
I just need to know why it behaves in this way?

Thanks and Regards,
Shiva RV

 
Best Answer chosen by Shiva Rajendran
Arun_KharbArun_Kharb
Hi Shiva,
Everything looks good to me except your action function.
Use a rerender attribute in your action function like this. Rerender is an important part for proper callback.
apex:actionFunction action="{!pageRedirectFn}" name="pageRedirectAF" rerender="YOUR_COMPONENT_ID">
            <apex:param name="pageData" id="pageData" value="3" assignTo="{!pageData}"/>

     </apex:actionFunction>
FYR - http://www.jitendrazaa.com/blog/salesforce/passing-parameter-in-actionfunction-in-visualforce/

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
 

All Answers

Arun_KharbArun_Kharb
Hi Shiva,
Everything looks good to me except your action function.
Use a rerender attribute in your action function like this. Rerender is an important part for proper callback.
apex:actionFunction action="{!pageRedirectFn}" name="pageRedirectAF" rerender="YOUR_COMPONENT_ID">
            <apex:param name="pageData" id="pageData" value="3" assignTo="{!pageData}"/>

     </apex:actionFunction>
FYR - http://www.jitendrazaa.com/blog/salesforce/passing-parameter-in-actionfunction-in-visualforce/

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
 
This was selected as the best answer
Shiva RajendranShiva Rajendran
Hi Arun ,
sorry for the delayed replay.yes it worked after i gave rerender.
Thanks and Regards,
Shiva RV