In the cose below I am fetching & displaying result set in a table format.
This table also contains 2 buttons Udate & Delete.
Following are the issues:
1. When user clicks update, I want to send E_ID to Update.jsp.
I want to know, how to send Only E_ID without using form.
2. When user clicks delete, that record is deleted and a message "Record Deleted" is displayed on the same page.Please Help

query="Select * from emp1";
rt=st.executeQuery(query);

out.println("<html><body><table border=\"1\">");
out.println("<tr> " +
		"<th> E_ID </th>" +
		"<th> E_Name </th>"+
		"<th> E_Address</th>" +
		"<th> E_Dept </th>"+
		"<th> Edit </th></tr>");
			
while(rt.next())
{
	out.println("<tr>" +
		"<td>" + rt.getString("E_ID") + "</td>" +
		"<td>" + rt.getString("E_Name") + "</td>" +
		"<td>" + rt.getString("E_Address") + "</td>" +
		"<td>" + rt.getString("E_Dept") + "</td>" +
		"<td> <input type=\"button\" name=\"Update\" value=\"Update\">"+
		"<input type=\"button\" name=\"delete\" value=\"Delete\"> </td></tr>");
}
			
out.println("</table><br/>");

Recommended Answers

All 3 Replies

AJAX / JavaScript to send a post request to another page.

I will give you a example for updation...

A small change in your code insert onclick event for update button as follows onclick="update(rt.getString("E_ID")); AND

a hidden paramter with updflag <input type='hidden' id='updflag'>

so when ever the user clicks on that button it will call javascript function which takes the eid value...

then passthis eid value to updateUser.jsp using AJAX...

IN AJAX Page ie., updateUser.jsp , after successful updation of the user assign the flag value as updated as follows document.getElementById('updflag')="<%=updflag%>"; After succussful updation of failure the above code will return as oXmlHTTP.responseText..


when you call eval(oXmlHTTP.responseText) , the hidden paramter updflag gets updated and then you can verify its value and keep an alert...

you can follow the same procedure for deletion also

query="Select * from emp1";
rt=st.executeQuery(query);

out.println("<html><body><table border=\"1\">");
out.println("<tr> " +
		"<th> E_ID </th>" +
		"<th> E_Name </th>"+
		"<th> E_Address</th>" +
		"<th> E_Dept </th>"+
		"<th> Edit </th></tr>");
			
while(rt.next())
{
	out.println("<tr>" +
		"<td>" + rt.getString("E_ID") + "</td>" +
		"<td>" + rt.getString("E_Name") + "</td>" +
		"<td>" + rt.getString("E_Address") + "</td>" +
		"<td>" + rt.getString("E_Dept") + "</td>" +
		"<td> <input type='button' name='Update' value='Update' onclick='update("+rt.getString("E_ID")+")'>"+
		"<input type=\"button\" name=\"delete\" value=\"Delete\"> </td></tr>");
}
			
out.println("</table><br/>");
out.println("<input type='hidden' id='updflag' value=''>")

out.println("<SCRIPT>
function update(eid){

var oXMLHTTP = new ActiveXObject( "Microsoft.XMLHTTP" );
	
// Prepare the XMLHTTP object for a HTTP POST to our validation JSP page
var sURL = "http://mysite/mypath/updateUser.jsp?E_ID=" + eid
oXMLHTTP.open( "POST", sURL, false );

// Execute the request
oXMLHTTP.send();
eval(oXMLHTTP.responseText);
if(document.getElementById('updflag')=="updated"){
        alert('updated');
}
else{

    alert('not updated');
}

}



")
updateUser.jsp

<%String eid=request.getParameter("E_ID")%>
<%/*Establish the connection to your database and consider con is the connection object*/

Statement stmt= con.createStatement();
String updflag="";
int cnt=stmt.executeUpdate("update emp set eid="+eid);
if(cnt==1)
  updflag="updated";
%>
document.getElementById("updflag")=<%=updflag%>;
commented: DB conenction from page, NO thank you -3
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="java.sql.*;" %> 
<%@ page import="java.io.*;" %>>
<%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Database Page</title>
    </head>

    <body background="AC.jpg">

  <a href="home.jsp"><font color="white"size="3"><b>Back to Home</b></font></a>
  <% try
{
 String a=request.getParameter("Book_Id"); 
 String b=request.getParameter("Book Name");
 String c=request.getParameter("Author name");
 String d=request.getParameter("Publisher name");
 String e=request.getParameter("Department");
 String f=request.getParameter("Edition");
 String g=request.getParameter("no_of_copy");

Connection con = DriverManager.getConnection("jdbc:odbc:proj","","");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query ="select * from BookMaster where Book_Id='" + a + "'";
ResultSet rs=stmt.executeQuery(query);
if(rs.next())
        {

                response.sendRedirect("BookMasterForm.jsp?result=Book_Id already exists");
                }
else
        {
            rs.updateString("Book_Id",a);
            rs.updateString("Book Name",b);
            rs.updateString("Author name",c);
            rs.updateString("Publisher name",d);
            rs.updateString("Department",e);
            rs.updateString("Edition",f);
            rs.updateString("no_of_copy",g);
            rs.insertRow();

        response.sendRedirect("BookMasterForm.jsp?result=Succesfully inserted");    
        }

rs.close();
stmt.close();
con.close();
}
 catch(Exception e)
        {   e.printStackTrace();
        }

%>


    </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.