Im having a VF page it has some text fields and one button . On clicking the button , the values in the text field should be passed to the controller class to save in the database . Can this be achieved . If yes , direct me .
2 Answers
i think this is one way
<apex:page showHeader="false" sidebar="False" controller="textInputsCon">
<apex:form >
Input Text1 <apex:inputText value="{!inputText1}"/>
Input Text2 <apex:inputText value="{!inputText2}"/>
<apex:commandButton value="save" action="{!saveList}"/>
</apex:form>
controller
public with sharing class textInputsCon {
public String inputText1{get;set;} // input text1 value from vf
public String inputText2{get;set;} // input text2 value from vf
public void saveList(){
salesObj salesObjInst = new salesObj(); // Either salesObj is standard or custom object
salesObj.field1 = inputText1; // Field Assignment
salesObj.field2 = inputText2; // Field Assignment
insert salesObj; // DML Operations
}
}
Another way , if you want call save action from the command button with out navigating to controller , you can use java script remoting.
Define one java script function same as a object, and call this function from the onclick event of button, check the below link
http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting_example.htm
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_methods.htm
You can reference the above document link to understand the concept of getter and setter in the sfdc.
Getter fetches the data from controller on to the visualforce page
Setters passes data from the visualforce page to apex controller class
Action methods are the methods invoked on the click of command button or link.
The order of execution is
1)All getters run first 2)Before your action methods all the setters get executed .
http://www.salesforce.com/us/developer/docs/workbook_vf/index.htm
Refer the workbook to get started with some basic examples and below wiki guide
http://wiki.developerforce.com/page/An_Introduction_to_Visualforce