Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript confirm box always returns true [duplicate]

Tags:

javascript

jsp

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'; 
}
like image 982
nallas Avatar asked Jun 01 '26 11:06

nallas


1 Answers

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.

like image 144
Pointy Avatar answered Jun 03 '26 01:06

Pointy