How to display "Confirmation box" in JSP?

hi all,
i try to display a confirmation box when clicking a button in JSP. but it does not seem to work.
pls help me solve this issue.
<%@ include file="/common/includes.jsp"%>
<script type="text/javascript">
<!--
function isProcess()
{
  var answer = confirm ("Are you sure you want to continue?");
  if (answer)
      return true;
    else
      return false;
}
// -->
</script> 
<% 
if (form.ButtonClicked("ok_button"))
  {
    if (form.ValidateFormFields())
    {
        boolean answer= true;
      if(busLogic.IsIdenticalTrans())
      {
        //get confirmation result
        answer=  isProcess();
         //when compile, it complains like "isProcess() is undefined type". what's going on? how to do it in a correct way? pls help!!!
      }
      if(answer)
      {
         //if confirmation is Yes,continue to do something 
       }
    }
  }
%>

Open in new window

LVL 6
viola123Asked:
Who is Participating?
 
rrzCommented:
Here is an example JSP.  If you need help changing it to your needs then ask us here.  
<html>
<head>
<script type="text/javascript">
<!--
function isProcess()
{
  var answer = confirm ("Are you sure you want to continue?");
  if (answer){
              document.myForm.answer.value = "true";  
              document.myForm.submit();
              return true;
    }else return false
 
}
// -->
</script> 
</head>
<body>
<form name="myForm" onsubmit="isProcess();">
<input type="submit" value="Continue"/>
<input type="hidden" name="answer"/>
</form>
<% 
   String answer = request.getParameter("answer");
   if(answer == null || "".equals(answer))answer = "false";
   if("true".equals(answer))out.print("Yes,continue to do something");
   else out.print("No, do not continue");
%>
</body>
</html>

Open in new window

0
 
rrzCommented:
I think you are mixing Javascript(happens in the browser at client) with JSP(at the server).    
If you want to know what the client answers, then you will have to submit a form ( or use AJAX) to the server.
Please tell us what you are trying to accomplish.  Maybe an expert will suggest a way to get done.
0
 
HonorGodSoftware EngineerCommented:
You are allowed, in JSP to have "HTML" code.  The "HTML" code that you include can contain stuff like:

<script type='text/javascript>
</script>

And the JavaScript can include a call to confirm()



var ans = null
 
if ( ans = confirm( 'Are you sure?' ) ) {
  alert( 'ok' )
} else {
  alert( 'cancel' )
}

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
viola123Author Commented:
hi ,
i try to put this javascript into a jsp file and reference it in my html page. but it still complains like "the method isProcess(String) is undefined for the type". i dont know how to solve it.

//in popup.jsp file:
//confirmation box
function isProcess(msg)
{
  var answer = confirm (msg);
  if (answer)
      return true;
    else
      return false;
}
********************************************
//in my main form contentHeader.jsp:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <!-- STYLESHEETS -->
    <link rel="stylesheet" href="/private/common/private_content.css" type="text/css" media="all" />
    <link rel="stylesheet" href="/private/common/private_print.css" type="text/css" media="print" />
    <!-- end stylesheets -->
 
    <!-- INCLUDED JAVASCRIPTS -->
    
    <script type="text/javascript" src="/private/common/popup.js"></script>
        <!-- end scripts -->
      </head>
  <body >
    <table width="100%" cellpadding="0" cellspacing="0" class="wrapper" valign="top">
	  <tr>
	  	<td colspan="2">
          <%@ include file="/common/header.jsp" %>
        </td>
      </tr>
	  <tr valign="top">
	    <td width="185">
          <%@ include file="/common/menu.jsp" %>
		</td>
	    <td align="left" style="padding-left:10px; padding-right: 10px; padding-bottom: 20px; width: 100%;">
*****************************************************
//in my content form myform.jsp:
<%@ include file="/common/includes.jsp"%>
<% 
if (form.ButtonClicked("ok_button"))
  {
    if (form.ValidateFormFields())
    {
        boolean answer= true;
      if(busLogic.IsIdenticalTrans())
      {
        //get confirmation result
        answer=  isProcess("Want to continue?");
         //when compile, it complains like "the method isProcess(String) is undefined for the type". what's going on? how to do it in a correct way? pls help!!!
      }
      if(answer)
      {
         //if confirmation is Yes,continue to do something 
       }
    }
  }
%>
 
<c:import url="/common/contentHeader.jsp" />
<form>
  //this is the form
</form>

Open in new window

0
 
viola123Author Commented:
hi all,
all i try to do is to popup a confirmation box on client side and get the answer on server side, so i can use the answer to decide if we need to go to next step or not. i know how to do it in c#/asp.net, but i am new to jsp/java. so need your help!!!!

thanks
viola
0
 
rrzCommented:
>all i try to do is to popup a confirmation box on client side and get the answer on server side,  
The confirm  is a javascript method.  When the user makes a decision, you can submit a form programmatically to the server. The server can do the next step and send a response to the client.
>i know how to do it in c#/asp.net  
JSP should be the similar.
0
 
viola123Author Commented:
rrz@871311:
could you pls give me an example?
when user click the submit button, the confirmation box pops up. if user choose YES, go to next step, If choose NO, then stay at current form.
thanks a lot
0
 
viola123Author Commented:
thanks heaps
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.