Confirm button in Visualforce page in Salesforce

Sample Code:

Visualforce page:

<apex:page controller="SampleController">
    <script>
        function callSave(){
            var r = confirm("Are you sure want to save?");
            if(r == true) {                
                callSav();
            }
        }
    </script>
    <apex:form >
        <apex:actionFunction name="callSav" action="{!sav}"/>
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!member.Name}"/>
                <apex:inputField value="{!member.E_Mail_Id__c}"/>
                <apex:inputField value="{!member.State__c}"/>
                <apex:inputField value="{!member.City__c}"/>
                <apex:inputField value="{!member.Age__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" onclick="callSave();"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex controller:

public class SampleController {
    public Member__c member {get;set;}
    
    public SampleController() {
        member = new Member__c();
    }
    
    public void sav(){
        insert member;
        member = new Member__c();
    }
}

Output:





2 comments:

  1. callSave() method calling repeatedly,If the confirmation is YES.I think the Save() method in callSave().

    ReplyDelete
    Replies
    1. callSav() will call sav() from apex.

      Cheers!!!

      Delete