0

In this code, when user adds delete button, raw is being deleted from database, but user is being redirected to empty home page, unless he logs out and logs in again. how can I fix the code, so that when he deletes something, whole layout is not disappearing, but only the line that he deleted.

<!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>




<script type="text/javascript">

function del() {
    if (confirm("Do You Want to Delete this Menu?")) {
    } else {
        return false;
    }
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="images/style.css" type="text/css"
charset="utf-8" />
</head>
<body>
<%

    menu_slno1 = request.getParameter("menu_slno");
    if (menu_slno1 != null)
        menu_slno = Integer.parseInt(menu_slno1);
    delete_menu = request.getParameter("delete_menu");


    if ("yes".equals(delete_menu)) {
        MenuId = request.getParameter("MenuId");
        x = stmt1
                .executeUpdate("Delete from menu where MenuId="
                        + MenuId);
    }
%>


<center><h2>VIEW MENU</h2></center>

<center><table width="736" height="97" border="1"></center>
    <%
        if (x == 1) {
    %>
    <tr bgcolor="gray">
        <th height="35" colspan="9"><div align="center">
                Menu deleted successfully!
            </div></th>
    </tr>
    <%
        }
    %>
    <tr bgcolor="gray">
        <td><div align="center">
                <strong>Menu ID</strong>
            </div></td>
        <td><div align="center">
                <strong>Name </strong>
            </div></td>
        <td><div align="center">
                <strong>Info</strong>
            </div></td>
        <td><div align="center">
                <strong>Price</strong>
            </div></td>
        <td colspan="2"><div align="center">
                <strong>Action</strong>
            </div></td>
    </tr>
    <%

     String sUserID=request.getParameter("username");
      session.setAttribute("username", sUserID);
        int icount = 0;
        rs = stmt.executeQuery("SELECT menu.menuID, menu.name, menu.info, menu.price, menu.RestaurantID  FROM menu INNER JOIN clients ON menu.username = clients.username where menu.username='" +sUserID+ "'");

        while (rs.next()) {
            //menu_slno = rs.getInt("menu_slno");
            MenuId = rs.getString("MenuId");
        %>
    <tr>
        <td><div align="center"><%=++icount%></div></td>

        <td><%=rs.getString("Name")%></td>
        <td><%=rs.getString("Info")%></td>
        <td><%=rs.getDouble("Price")%></td>

        <td><div align="center">
                <a href="edit_menu.jsp?MenuId=<%=MenuId%>">Edit</a>
            </div></td>
        <td><div align="center">
                <a
                    href="view_menu.jsp?delete_menu=yes&MenuId=    <%=MenuId%>&MenuId=<%=MenuId%>"
                    onclick="return del()">Delete</a>
            </div></td>
    </tr>
    <%
        }
    %>
</table>
<a href="add_menu.jsp">Add Menu</a>

</body>
</html>
G.M
  • 653
  • 1
  • 15
  • 35

1 Answers1

1

When a user clicks on the delete link, the username parameter is not set in the URL. So when the page refreshes, the String sUserID=request.getParameter("username"); call return null or empty, and then the rest of the page is not displayed because the following SQL Query returns nothing without a valid username.

It seems that you only need to add something like &username=<%=sUserID%> to your delete link to make it work:

<a href="view_menu.jsp?delete_menu=yes&MenuId=<%=MenuId%>&username=<%=sUserID%>" onclick="return del()">Delete</a>

P.S: I assume that this a legacy JSP, and that you have no choice but continuing using such ugly code. If you have a choice, I strongly recommend you to drop those scriptlets and this all-in-the-view architecture, and start using JSTL tags, servlets and beans.

Med
  • 628
  • 9
  • 29
  • Thanks a lot, it worked. And you a right, I have no choice but using such an ugly code. – G.M Jan 09 '13 at 09:18