3

This is my code for confirmation box. When i press OK it should call the delete function. When I press Cancel it should return to the home page, but the file is deleted even though I choose Cancel. This is my code. What is wrong ?

var response = confirm ("Are you sure you want to permanently delete this user?");  

if (response)  
{  
  <% String userName =request.getParameter("userName");      
     del.del(userName);
   %> 
  window.location.href='adminHome.jsp';
}
else
{
  window.location.href='adminHome.jsp'; 
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
nallas
  • 69
  • 2
  • 8

1 Answers1

5

This code:

<% String userName =request.getParameter("userName");
del.del(userName);%>

runs on your server before the page is even sent to the browser, and therefore the user is gone before you even get to the confirm().

You'll have to introduce an explicit new HTTP request, either by simply posting a form or else via ajax, and handle that at the server. That request would only be triggered when your confirmation returns true.

Pointy
  • 405,095
  • 59
  • 585
  • 614