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
Kunal Purohit 4Kunal Purohit 4 

How to display confirmation message after clicking Send button?

Hello Folks,
I want to display a confirmation message "Message Submitted Successfully", with OK button. I am trying but before sending message, it is generating an alert and after that SMS is being sent. Here is my Visualforce page code. Please suggest.

<apex:page showHeader="false" controller="ProspectUserSMSMessageController">
    <script language="JavaScript" type="text/javascript">
        function ClosePage(){
            window.top.close();
        }
    </script>
    <apex:outputPanel id="redirectionBlock">
        <apex:pageMessages />
        <script>
        var hasMessages = '{!hasError}';
        if(hasMessages == 'No') {
        	window.top.close();
        }
        </script>
        <script> 
        		function Show_Alert()
                {
                var msg = "Message submitted successfully";
                alert(msg);
                }
		</script>
    </apex:outputPanel>
    <apex:form >
        <apex:pageBlock title="Send SMS" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!sendSMS}" reRender="redirectionBlock" value="Send SMS" onclick="Show_Alert()"/>
                <apex:commandButton onclick="javascript:ClosePage()" value="Close"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" showHeader="false">
                <apex:pageBlockSectionItem labelStyle="text-align:right;">
                    <apex:outputLabel value="Name : " />
                    <apex:outputLabel value="{!fullName}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Phone No : " />
                    <apex:outputLabel value="{!phoneNumber}" />
                </apex:pageBlockSectionItem>
                <apex:actionFunction name="displayTemplate" action="{!showContent}" reRender="smsMessage"/>
                <apex:pageBlockSectionItem >
                    <apex:outputlabel value="Choose Template : " />
					<apex:selectList value="{!selectedItem}" size="1" onchange="displayTemplate();">
						<apex:selectOptions value="{!templateList}" />
                    </apex:selectList>
				</apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Message : " />
                    <apex:outputPanel id="smsMessage">
                    	<apex:inputTextArea cols="50" rows="5" value="{!smsMessage}"/>
                    </apex:outputPanel>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
AnkaiahAnkaiah (Salesforce Developers) 
Hi Kunal,

Refer the below link to display toast message in VF page.
https://www.eternussolutions.com/2019/04/03/now-toast-your-messages-from-visualforce-pages-too/

If this helps, Please mark it as best answer.

Thanks!!
Shri RajShri Raj


To display a confirmation message after clicking the Send button, you can use JavaScript's alert() function. Here's how to modify your code:

<apex:commandButton action="{!sendSMS}" reRender="redirectionBlock" value="Send SMS" onclick="Show_Alert()"/>
 
<apex:commandButton action="{!sendSMS}" reRender="redirectionBlock" value="Send SMS" onclick="Show_Alert(); return false;"/>
 
<script> 
    function Show_Alert()
    {
        var msg = "Message submitted successfully";
        alert(msg);
    }
</script>


This way, when the user clicks the Send SMS button, the JavaScript function Show_Alert() will be executed, displaying the confirmation message "Message submitted successfully". The return false; in the onclick event is used to prevent the form from submitting before the alert is displayed.