+ Start a Discussion

How can I pass a inputfield value into a controller variable and query.

I am trying to use the code below to assign the value from an inputfield into a variable within my controller to then use that value in a query.  Please could someone tell me what I need to enter to assign that data

<apex:inputField value="{!emp2.Paynumber__c}" id="PaySearch" /> <p/> 

public class EmployeeList {

String PayN = '15154';

    public PageReference update1() {
        PayN = {!$component.PaySearch);
        return null;
You should not use javascript to do this -- that will unnecessarily complicate things.  When your action method is called (update1) all of the setters will be called first.  So if you have an inputField with a reference to {emp2.Paynumber__c} then it will set that field on the emp2 object in your controller.  Something like this:

  <apex:inputField value="{!emp2.Paynumber__c}" id="PaySearch"/>
  <apex:commandButton value="Update" action="{!update1}"/>

public class myController {
  public emp2__c emp2 {get; set;}

  public myController() {
    emp2 = [select paynumber__c from emp2__c limit 1];

  public PageReference update1() {
    String payNumber = emp2.paynumber__c;
    //do your other query here.


 So when you click on the command button the first thing it does is call the emp2 setter, which will update all of the fields (in this case, just paynumber__c) on that object.  Then your action method gets called, and you can just access the field directly off of the emp2 object.

Hi Jwetzler,
I'm having the same question, I followed your suggestion and add the System.Debug in the Update1 function then I got the value of the paynumber__c retrieved from the Select in the Constructor and not the value selected by the user. Do you have any idea how can I get the value selected by the user when using of inputField?