-1

I want to delete a user from my system but it just show an alert message with 'OK' button on both success or fail message. I have 2 related .java file whereas User.java (that declare the userID) and UserManager.java (that connect the java with mysql database and to manipulate data such as method deleteAUserFromDB() that u can find also in the jsp file below).I have also 2 jsp file that related to that java file whereas the form page and the action file. Below is the code that i'd apply on my action file:

deleteUserAction.jsp:

<%@ page import="java.lang.*" %>
<jsp:useBean class="spkp.UserManager" id="userManager" />
<jsp:useBean class="spkp.User" id="userToDelete" />
<jsp:setProperty name="userToDelete" property="userID"/>

<%boolean success=userManager.deleteAUserFromDB(userToDelete);
if(success){%>
  <script>
    alert('Success to delete!!!');
    window.location='deleteUser.jsp';
  </script>
<%}else{%>
  <script>
    alert('Fail to delete!!!');
    window.location='deleteUser.jsp';
  </script>
<%}%>

before that, deleteUser.jsp is just a form page where it shows the list of userID (using arrays) and the 'Delete' button to each information shown and below is the code to send the information to the deleteUserAction.jsp:

deleteUser.jsp

 <jsp:useBean class="spkp.UserManager" id="userManager" />
<%@page import="spkp.User"%>
<%@ page language="java" import="java.lang.*" import="java.sql.*" %>
<jsp:useBean class= "spkp.MySQLConnection" id= "view" />
<html>
<head>
</head>
<body>
  <table width="80%" align="center" border="1" bordercolor="#000000">
        <tr>
            <td width="200" height="28">UserID</td>
            <td width="100">Delete User</td>
        </tr>
    <%
    User[] allUsers = userManager.getAllUsersFromDB();
    if(allUsers!=null)
        {
    for(int i=1;i<allUsers.length;i++)
        {
        %>
        <tr>
            <td><%= allUsers[i].getUserID()%></td>
            <td align="center"><input type="button" value="Delete" onclick="window.location='deleteUserAction.jsp?userID=<%= allUsers[i].getUserID()%>'"/>
            </td>
        </tr>
        <% } } %>
     </table>
   </body>
   </html>

I have found many Javascript code for Confirmation Dialog Box that can apply in JSP such as: link1 and link2

but they just show the box with no function whether we click yes/no but not for delete any of the user from my database that I choose. I do try my best search for solution in a few day for this but I'm really stuck.

Eja
  • 1
  • 1
  • 1
  • 4
  • I don't see any code that actually deletes the user from DB – jmj Dec 17 '11 at 15:34
  • look at deleteUserAction.jsp, booleansuccess=userManager.deleteAUserFromDB(userToDelete); it retrieve information from my UserManager.java where there are a method deleteAUserFromDB(). – Eja Dec 17 '11 at 15:38
  • 1
    What's the actual question? How to make the button pop up the dialog box and either go to the next JSP or stay on the same page? Sure haven't seen JSP written like this for quite awhile; have you considered using a more modern approach? – Dave Newton Dec 17 '11 at 15:39
  • Dave Newton- the actual question, i want when i click Yes on the confirmation box, it actually delete the data from the database but if i click No, then no process. it has 2 jsp page here, one is a form page then another is just an action page without using code. the form will go to the action page then the action will delete the data. from my coding, they just an alert,once you click 'Delete' button on the form page and then click OK on alert popup, then the data is deleted. how if i want to click 'Delete' but i want to cancel that process?? do u understand what i try to say? – Eja Dec 17 '11 at 15:48
  • @Eja can you edit/update the question details with the question? Its not immediately clear to someone first looking at this. – Brian Deragon Dec 17 '11 at 15:51
  • Dave Newton-the dialog box is retrieve from : – Eja Dec 17 '11 at 15:53
  • ok brian, i'll do my best :-) – Eja Dec 17 '11 at 15:56

1 Answers1

1
<td><%= allUsers[i].getUserID()%></td>
<td align="center"><input type="button" value="Delete" onclick="comfirm_decision('<%= allUsers[i].getUserID()%>');"/>

between script tag

function confirm_decision(user_id){
    if(confirm("you want to delete the user?")) // this will pop up confirmation box and if yes is clicked it call servlet else return to page
     {
       window.location="deleteStaffAction.jsp?userID="+user_id; 
     }else{
       return false;
    }
   return true;
 }

file1 enter image description here enter image description here confirm window

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • just try again its working fine in my system.. check if you are using exactly same code. – dku.rajkumar Dec 17 '11 at 15:59
  • i have added the screen shot for your reference. – dku.rajkumar Dec 17 '11 at 16:11
  • I understood that the OP actually don't want to show a confirmation message *before* deleting, but an informal/error message depending on whether the delete was successful or not. Your answer however assumes the first case (which is indeed the normal and most sensible case). – BalusC Dec 17 '11 at 16:42
  • that is easier than this.. you wanna show message in next redirected page or current page. – dku.rajkumar Dec 17 '11 at 16:47
  • u know what kumar..there something i dont understand in your answer. why does the 'DELETE' button not together in the same form page of 'user id is equal to 5'? how would i wanna delete that user id if the button does not there? then how would the confirmation message will change the data in the database? – Eja Dec 19 '11 at 03:02
  • hey thats the redirected page after the deletion. you can show the message like "user with id 5 has been deleted" In that page i have just received the user id sent from previous page and displayed to confirm that the parameter passed is recieved. – dku.rajkumar Dec 19 '11 at 03:08
  • rajkumar, thanks a lot for your help. now my problem is solved. but i wonder, how to change the word 'OK' into 'Ya' and 'Cancel' into Batal' since that words in confirm message is auto-create. it is just wondering, but really thankful to you rajkumar – Eja Dec 23 '11 at 17:27
  • You can do that easily using jQuery confirm box.. Anyway You are welcome buddy.. if it helped you solve your problem dont forget to mark it as accepted answer for the help of future visitors. – dku.rajkumar Dec 23 '11 at 17:54
  • how to accept? by giving a feedback 'Yes' for " was this post useful to you?" i'm new to the stackoverflow.some buttons here were a bit confusing. – Eja Dec 24 '11 at 05:23
  • just below upvote and downvote arrow. there will be a tick mark., click that to accept the answer. – dku.rajkumar Dec 25 '11 at 14:02