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
CB312CB312 

Passing an HTML input to Apex controller

Hi,

 

I have an html input that I would like to pass to my apex controller. How can I have APEX access the value?


Sanitized code below, I am trying to pass the "rep2" field to my controller.

 

Sanatized code here, I'm using JQuery Autocomplete to populate the field.

VF Page:

 

$j(document).ready(function() 
    {
        $j("#rep2" ).autocomplete({
            'source': replist
        });
     });   

<apex:form >
<apex:pageBlock >

   <div class="ui-widget">
    <label for="rep2"><b>Reassign to: </b></label>
    <input id="rep2"/>
   </div>

<apex:pageBlockButtons >
        <apex:commandButton action="{!updateLeadOwner}" value="Save"   rerender="leadtoupdatetable"/>
       </apex:pageBlockButtons>
      </apex:pageBlock>

</apex:form>

 Controller:

public pagereference updateLeadOwner()
    {
        id leadid = ApexPages.currentPage().getParameters().get('id');
        string repname = Apexpages.currentPage().getParameters().get('rep2');
system.debug('-----------------DEBUG-------------');
        system.debug(repname);

 repname return null, what is the best way to access the value?

 

Thanks!

 

-Chris

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

try this , I have picked your example and used a apex:inputHidden and assigned it value of your html input and passed it to contrller

 

Contrller : In this i used add error just to show the value in pageMessage so that it confirms that value is passed

public class CommandButtonApexParam {

    
    public String rep2{get;set;}
    public PageReference updateLeadOwner() 
    {
        new account().addError('*****'+ rep2);
        return null;
    }

}

 

 

VFP : In java script function i used the id for hidden in my example if it is something else then change in your case, pick it from view source.

 

<apex:page controller="CommandButtonApexParam" id="pgId">
<script>
    function setHidden()
    {
        var hiddenRep = document.getElementById('pgId:frmId:pgBlockId:hdnRep2');
        hiddenRep.value = document.getElementById('rep2').value;
    }
</script>
<apex:form id="frmId">
<apex:pageMessages id="pgMsg"></apex:pageMessages>
<apex:pageBlock id="pgBlockId">

   <div class="ui-widget">
    <label for="rep2"><b>Reassign to: </b></label>
    <input id="rep2"/>
   </div>

    <apex:inputHidden id="hdnRep2" value="{!rep2}"/>    
    <apex:pageBlockButtons >
        <apex:commandButton action="{!updateLeadOwner}" onclick="setHidden();" value="Save" rerender="pgMsg,leadtoupdatetable">
        
        </apex:commandButton>
       </apex:pageBlockButtons>
      </apex:pageBlock>

</apex:form>
</apex:page>

 You can replicate this for you, let me know if any issues in it.

All Answers

aballardaballard

Why dopn't you use an apex:inputText for the field and bind it to a controller property?    That's the usual way to get data from the page to the controller.   Should b e tons of examples in our doc....

CB312CB312

Thanks but the reason I am not using Apex:input text is I want to leverage the JQuery Autocomplete function and from what I've read (and tried) it doens't work with apex.

 

ie. if I change

   <div class="ui-widget">
    <label for="rep2"><b>Reassign to: </b></label>
    <input id="rep2"/>
   </div>

 to:

   <div class="ui-widget">
   <apex:inputText value="{!inputValue}" id="rep2"/>
   </div>

 the JQuery autocomplete functionality breaks. Any other ideas, either how to get JQuery to work with Apex components or how to get the input value?

 

-Chris

Shashikant SharmaShashikant Sharma

try this , I have picked your example and used a apex:inputHidden and assigned it value of your html input and passed it to contrller

 

Contrller : In this i used add error just to show the value in pageMessage so that it confirms that value is passed

public class CommandButtonApexParam {

    
    public String rep2{get;set;}
    public PageReference updateLeadOwner() 
    {
        new account().addError('*****'+ rep2);
        return null;
    }

}

 

 

VFP : In java script function i used the id for hidden in my example if it is something else then change in your case, pick it from view source.

 

<apex:page controller="CommandButtonApexParam" id="pgId">
<script>
    function setHidden()
    {
        var hiddenRep = document.getElementById('pgId:frmId:pgBlockId:hdnRep2');
        hiddenRep.value = document.getElementById('rep2').value;
    }
</script>
<apex:form id="frmId">
<apex:pageMessages id="pgMsg"></apex:pageMessages>
<apex:pageBlock id="pgBlockId">

   <div class="ui-widget">
    <label for="rep2"><b>Reassign to: </b></label>
    <input id="rep2"/>
   </div>

    <apex:inputHidden id="hdnRep2" value="{!rep2}"/>    
    <apex:pageBlockButtons >
        <apex:commandButton action="{!updateLeadOwner}" onclick="setHidden();" value="Save" rerender="pgMsg,leadtoupdatetable">
        
        </apex:commandButton>
       </apex:pageBlockButtons>
      </apex:pageBlock>

</apex:form>
</apex:page>

 You can replicate this for you, let me know if any issues in it.

This was selected as the best answer