+ Start a Discussion
anu_karthianu_karthi 

Passing input text value from visualforce page to controller

Hi I have a vpage as follows..


<apex:outputLabel style="font-weight:bold;" value="Search By Branch Code" ></apex:outputLabel>
        <apex:inputText value="{!textData}"/>

 

Iam trying to pass this input textfield value(what ever i enter) to the controller by using getters and setters but iam not able to pass the value its passing  just anull value

 

Getters and setters as below

 

private String textdata = null;

    public String getTextData() { return textdata; }
    public void setTextData(String data) { textdata = data; }
 

 So Some one plz help me in passing myy in put field value to controller so that i want to use that value in my soql query

 

Eg:

CAF_Bank__c [] currentbanknames=[SELECT  Bank_Name__c FROM CAF_Bank__c  where Branch_Code__c=:textdata];

            So plz tell me with an example.............

 

       

 

                                                                                                    Thanks& Regards

                                                                                                          Anu...

bob_buzzardbob_buzzard

Change your textdata declaration to make it public - at the moment you have something that looks like a property, but is private.  Even though you have public getter/setter, this seems to confuse VF.

 

Also, as you aren't doing anything special in the getter/setter, you can shorten your code to:

 

 

public String textdata {get; set;}

 

 

 

anu_karthianu_karthi

Hi Bob

Thanks for your reply ,i have tried as if u suggested but not able to doit.Actually my requirement is to pass a inputtext value from visualforce page to apex controller so to use that field value in where clause of a soql query...So As iam not familiar in doing that ,please provide me with a basic example of doing that ..

 

 

 

                                                                                                                Thanks and regards,

                                                                                                                            Anuu.....

                               

bob_buzzardbob_buzzard

When you say you couldn't do it, was that to make the property public or to shorten the code to declare get/set rather than define methods?

 

Here's an example nearer to your original, including using it in SOQL.

 

 

Page: <apex:inputText value="{!textData}"/> Controller: public String textData; public String getTextData() { return textData; } public void setTextData(String newTD) { textData=newTD; } public void UseTextData() { List<Account> accounts=[select Id, Name from Account where Name=:textData]; }

 

 

 

anu_karthianu_karthi

Hi Bob,

 

  Thanks for your reply i have tried in the same way but iamnot able to do that  ..PLz check my code

My page asfollows

 

<apex:page standardController="CAF_Bank__c" extensions="MyClass13">
<script>
         function autopopulating()
         { 
            function trim(text)
              {
                text = text.replace(/^\s+/, '');
                return text.replace(/\s+$/, '');
              }
          var bc=document.getElementById('{!$Component.form1.iField}').value;
          var myArray2 = new Array();
          var myArray3 = new Array();
           myArray2 = '{!BankNameList}'.replace('[','').replace(']','').split(',');<!-- here iam passing my list which contains bankname  to the array-->

alert(myArray2);<!--giving a strange output iam not gettinng exact value-->

           myArray3= '{!CurrentBankList}'.replace('[','').replace(']','').split(',');
           alert('array initialized');
           var j=0;
            var currbankname=myArray3;


alert(myArray3);<!--output alert is punjab(bankname ) whose branchcode =982 which i entered as a input valuein my page since i hardcoded my query in the controller i could get the correct value here -->

         for ( j=0; j< myArray2.length; j++)
   {
    if(trim(myArray2[j]) == currbankname)
     {
       var y=myArray2[j];
      document.getElementById('{!$Component.form1.name}').value=currbankname
       document.getElementById('{!$Component.form1.iField}').focus();
     return  branchpopulate();
    return false;
     
     }
     }
    }
    
  function branchpopulate()
  {
 var myArray4 = new Array();
          var myArray5 = new Array();
           myArray4 = '{!BranchNameList}'.replace('[','').replace(']','').split(',');
          myArray5= '{!CurrentBranchList}'.replace('[','').replace(']','').split(',');
           alert('array initialized');
           <!--var currbankname={!currentbanknames};-->
           var k=0;
            var currbranchname=myArray5;
                  alert(currbranchname);
                  alert(myArray4);
                  alert(myArray5);
            for ( k=0; k< myArray4.length; k++)
   {
    
    if(trim(myArray4[k]) == currbranchname)
     {
    
       
       alert('array is fetched');
       var z=myArray4[k];
      document.getElementById('{!$Component.form1.bname}').value=currbranchname;
       alert('finalstep');
        
           alert(myArray4[k]);
       document.getElementById('{!$Component.form1.iField}').focus();
        alert(myArray4[k]);
                   alert(myArray4);
                   alert(myArray5);
       return false;
     }
     }  
  }  
function trim(text)
 {
  text = text.replace(/^\s+/, '');
  return text.replace(/\s+$/, '');
 }

</script>

<apex:form id="form1" >
<apex:actionFunction name="fun1" action="{!save}"/>
<apex:actionFunction name="changingmode" action="{!newmode}"/>
<apex:actionFunction name="nayamode" action="{!newmode2}"/>

<apex:actionFunction name="newmodel" action="{!newmode3}"/>
<table width="100%">
      <tr>
      <td width="19%"></td>
     
      <td align="left" valign="top" height="45"><font style="font-family:Tahoma, Geneva, sans-serif; font-size:18px; line-height:24px; color:#900;"><apex:outputText value="Bank Master Maintainance" rendered="true"  ></apex:outputText></font>
      </td>
      <td></td>
  </tr></table>
 
     <table width="100%"> <tr>
      <td width="5%"></td>
     <td align="left" valign="top" height="45"><font style="font-family:Tahoma, Geneva, sans-serif; font-size:12px; line-height:24px; color:#900;"><apex:outputText value="{!mode}" rendered="true"  ></apex:outputText></font>
      </td>
      <td></td>
  </tr></table>
 
   <apex:panelGrid columns="1">
    <apex:outputLabel style="font-weight:bold;" value="Search By Branch Code" ></apex:outputLabel>
        <apex:inputText value="{!userinput}"/><!--iam trying to pass this value to ciontroller so thatt i can use this value in my query-->      

</apex:panelGrid>
  </tr></table>                                      
<table width="100%">
    <tr>
      <td width="5%"></td>
      <td>
          <table width="100%" align="center">
         <tr>
                   <td height="30" width="170px"><apex:outputLabel value="Bank Name">
     </apex:outputLabel></td>
                   <td><apex:inputField id="name" value="{!CAF_Bank__c.Bank_Name__c}">
                    <apex:actionSupport event="onkeyup"  action="{!disableEnable}" rerender="buttonSection"/>
                   </apex:inputField>
                   </td>
               </tr>
               <tr>
                   <td height="30"><apex:outputLabel value="Branch Code">
      </apex:outputLabel></td>
                   <td><apex:outputPanel id="branchcoding">
     <apex:inputField id="iField" value="{!CAF_Bank__c.Branch_Code__c}">
      <apex:actionSupport event="onkeyup" action="{!disableEnable}" rerender="buttonSection"/>
      </apex:inputfield>
      </td></tr>
      <script>document.getElementById('{!$Component.iField}').disabled = {!disableInput}; </script>
      </apex:outputPanel> 
              
               <tr>
                   <td height="30"><apex:outputLabel value="Branch Name">
     </apex:outputLabel></td>
                   <td><apex:inputField id="bname" value="{!CAF_Bank__c.Branch_Name__c}">
                    <apex:actionSupport event="onkeyup" action="{!disableEnable}" rerender="buttonSection"/>
                   </apex:inputfield>
                   </td>
                 </tr>
               <table align="left"><tr><td align="center">

</td></tr></table>

</td><td width="5%"></td></tr>

</table>
<table width="100%"> <tr>
      <td width="12%"></td>
      <td align="left" valign="top" height="45">
        <apex:outputpanel id="buttonSection">
    <apex:commandButton disabled="{!isNewButtonDisabled}" value="New" action="{!newrecord}" />
      <apex:commandButton disabled="{!isEditButtonDisabled}" value="Edit" action="{!disableCloseDateInput}" oncomplete="focussing()" rerender="branchcoding, buttonSection"/>
       <apex:commandButton disabled="{!isSaveButtonDisabled}" id="button2" action="{!save }" value="Save" onclick="return validate()" reRender="form1" />
   <apex:commandButton value="autopopulating" onclick="autopopulating()"/>
  </apex:outputpanel>
      </td>
      <td></td>
      </apex:form>
  </tr></table>
</apex:page>

 

ApexClass :

 

public class MyClass13
{
public String userinput;
List<String> BankBranchList = new List<String>();
List<String> BankNameList=new List<String>();
List<String> currentbanknames=new List<String>();
List<String> CurrentBankList=new List<String>();
List<String> BranchNameList=new List<String>();
List<String> CurrentBranchList=new List<String>();
ApexPages.StandardController con;
public MyClass13(ApexPages.StandardController controller){ 
this.con = controller;
CAF_Bank__c[] branches = [SELECT Branch_Code__c FROM CAF_Bank__c];
for (CAF_Bank__c b : branches) {
BankBranchList.add(b.Branch_Code__c);
}
CAF_Bank__c [] banknames = [SELECT  Bank_Name__c FROM CAF_Bank__c];

for (CAF_Bank__c c : banknames)
{
BankNameList.add(c.Bank_Name__c);
}
/*trying to fetch the value of the bank name where bankname correspondind to current branchcode*/
CAF_Bank__c [] currentbanknames=[SELECT  Bank_Name__c FROM CAF_Bank__c  where Branch_Code__c=:userinput];   /* here i want to query my object to retrieve bankname field where branchcode==my passedvalue, */

for (CAF_Bank__c d :currentbanknames)
{
CurrentBankList.add(d.Bank_Name__c);/* ading my queried output to the list and ipass this list to array in my vpage*/

}
CAF_Bank__c [] branchnames = [SELECT  Branch_Name__c FROM CAF_Bank__c];
for (CAF_Bank__c e : branchnames)
{
BranchNameList.add(e.Branch_Name__c);
}
/*trying to fetch the value of the branchk name where bankname correspondind to current branchcode*/
CAF_Bank__c [] currentbranchnames=[SELECT  Branch_Name__c FROM CAF_Bank__c  where Branch_Code__c='982'];<!-- here i hardcoded the value justt for tersting here iam getting the correct valuefor branchname field whose branchcode =989--> (CAF_Bank__c f :currentbranchnames)
{
CurrentBranchList.add(f.Branch_Name__c);
}
}
public String[] getBankBranchList() {
return BankBranchList;
}
public String[]  getCurrentBankList() {
return CurrentBankList;
}
public String[] getBankNameList() {
return BankNameList;
}
public String[] getBranchNameList() {
return BranchNameList;
}
public String[] getCurrentBranchList() {
return CurrentBranchList;
}
/*getters and setters of userinput*/
public String getuserinput(){return userinput;}
public void setuserinput(String userinp){this.userinput=userinp;}  /* using gettersand setters to pass my input value to controller*/

public void save() {
try
{
con.save();
}
catch(Exception ex1)
{
}
}
}

 

Now instead of hard coding i want to pass  the value from page to controller to use that value in where clause,,so please someone help me in doing this......

 

                                                                                                                                      Thanks& Regards,

                                                                                                                                            Anu.........

 

 

bob_buzzardbob_buzzard
So what problem are you seeing?  Is it that the user input is not being carried through, or do you have problems with your javascript?
anu_karthianu_karthi

Hi Bob,

 

 Iam thankful  for ur kindful help.......

 

CAF_Bank__c [] currentbanknames=[SELECT  Bank_Name__c FROM CAF_Bank__c  where Branch_Code__c=:userinput];  
here iam queirng for bankname whose branchcode to be equal to my user input.but iam not getting my corresponding  bankname here (since i check this in myjavasdript by adding the queried output to the list and passing the list to javascript and i try to find the values inside the list by passing the list to an array and raised a alert box by passing this array..it is giving me some bankname values but not a single value(whose branchcode!user input)
so i feel the userinput is not passed and that is why iam not able to query it here what i want is i should get bankname corresponding to a branchcode ==userinput(input text field value..just like a search button but in search button i will display the record in datatable ..but here i want to pass the list to the javascript...
so i feel like iam not able to passs userinput value.....

Thanks for support in helping as i just started learning apex and visualforce pages this issue is looking strange and iam not able to solve since many days..please support me,if u r not clear with problem which i facing please  let meknow,i will make it clear..

 Expecting a solution from u ........


                                                       Thanks & Regards
                                                              Anu.......

bob_buzzardbob_buzzard

There's quite a lot of code there, so I'm struggling to identify what is what.

 

Can you post a reduced set of code containing just the sections that you use your inputText, including where you use it in JavaScript? 

 

Also, System.debug is a good friend when coding VF/Apex - add some debug statements around your method that looks up the branchcode for the user input and log out the userinput plus the results.  That is the only way to be absolutely sure what is happening.

vikiviki

Thanks Bob; your code helped !! :smileyvery-happy:

CodeFinderCodeFinder

Hi Bob,

 

Can you please help me with this? I cannot understand what my mistake is.

 

Here is my post which is related to to the same type of problem.

 

http://boards.developerforce.com/t5/Apex-Code-Development/get-input-text-value-from-pageblocktable/td-p/548001

bob_buzzardbob_buzzard

I've posted a response to that thread.

nishad basha 7nishad basha 7

Hi, bob_buzzard

How to display the duplicate error Message once create a user using visualforce page?
 
Jigar TrivediJigar Trivedi
Hi Anu,

try this solution if your problem stated in you question still persists.

This is my VF page where I am passing username and password values to my Apex class:
<apex:inputText label="Username: " value="{!username}" id="txtusername"/>
<apex:inputSecret label="Password: " value="{!password}" id="txtpassword"/>
and this is my Apex class:
 
public string usename{get;set;}
public string pasword{get;set;}

    public string getUserName(){
        return usename;
    }
    public void setUserName(string s){
        usename=s;            
    }    
    public string getPassword(){
        return pasword;
    }
    public void setPassword(string s){
        pasword=s;            
    }
and this is the query in which I am using these values:
 
lstLogin = [select id, username__c, password__c from Login__c where username__c= :usename and password__c= :pasword];
I hope this works out for you. Please tell me if you got any doubt.


 
saikishorecareer3341.3947811933764766E12saikishorecareer3341.3947811933764766E12
Try this will work

Page :

<apex:inputText label="UserName" value="{!EnteredText}" />

Controller :

Public string EnteredText{get;set;}
 
Pathan S 4Pathan S 4
I want to a visualforce code and cotroller.When i entered data then it will be shown under the field @output feild.plz help.? Mw PathanUser-added imageUser-added image