function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandeep YadavSandeep Yadav 

I stucked to pass the inputfiled value to the controller using javascript

Hi everyone,
Here is my code--
when I debug it shows null value.
Any suggestion ??
<apex:page controller="AlertConId" >
  
  <script type="text/javascript">
      
      function demo(fst,lst)
      {
          var fn = document.getElementById(fst).value;
          var ln = document.getElementById(lst).value;
          myFunction(fn,ln);
      }
  </script>
  
  <apex:form >
      <apex:actionFunction name="myFunction" action="{!save}">
          <apex:param value="" name="fname" assignTo="{!fname}"/>
          <apex:param value="" name="lname" assignTo="{!lname}"/>
      </apex:actionFunction>
      
      <apex:pageBlock id="ref">
          <apex:pageBlockSection >
              <apex:inputText id="fname" label="FirstName"/>
              <apex:inputText id="lname" label="LastName"/>
              
              <apex:commandButton value="Go" onclick="demo('{!$Component.fname}','{!$Component.lname}')" reRender="ref"/>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

------------------------------------------------------------------------------------------------------------

public class AlertConId 
{
    public Contact ct {get;set;}
    public string fname {get;set;}
    public string lname {get;set;}
    
    public void save()
    {
        string first = ApexPages.currentPage().getParameters().get('fname');
        string last = ApexPages.currentPage().getParameters().get('lname');
        system.debug(first + last);
        
        ct = new Contact();
        ct.firstname = first;
        ct.lastname = last;
        try{
            insert ct;
        }
         catch(Exception e){}
    }
}

 
Vinod ChoudharyVinod Choudhary
Try:
 
function demo(fst,lst){
   jQuery('[id$=fst]').val(param);
    jQuery('[id$=lst]').val(param);
    demo();
}

Hope this will help yo.

Thanks
Vinod 
Abhishek BansalAbhishek Bansal
Hi Sandeep,

When you have set the value in the variables with the help of the param then why did you tried to get it from the URL. You should directly use the variables as set in the page. Please change your code as below :
public class AlertConId 
{
    public Contact ct {get;set;}
    public string fname {get;set;}
    public string lname {get;set;}
    
    public void save()
    {
        // In your VF page you have set the vaues in the variables fname and lname so directly use them
        system.debug(fname + lname);
        
        ct = new Contact();
        ct.firstname = fname;
        ct.lastname = lname;
        try{
            insert ct;
        }
         catch(Exception e){}
    }
}

Please let us know if you need any further information or help on this.

Thanks,
Abhishek Bansal.