sir, I tried ur code but when i click on cancel it still submits
I dont need to pass id as record is already diaplayed using ajax and i used it in delete button like this
<form method="post" id="myform" name="myform" action="delete_sup"> <center> <h3>Delete Supplier OR Edit Supplier</h3> </center><br><br><br> <font color=""> <center> <table border="0px" style="border-width: 2px; border-color:#4E387E; border-style: solid;border-spacing: 6px" bgcolor="#CCFB5D"> <div id="mydiv"></div> <tr> <td> Supplier ID </td> <td> <select name="suplierid" onchange="showEmp(this.value);"> <option value="-1">Select</option> <% Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "mca"); Statement stmt1 = con1.createStatement(); ResultSet rs2 = stmt1.executeQuery("Select s_id from supplier"); while (rs2.next()) { String sup = rs2.getString(1); %> <option value="<%=sup%>"><%=sup%></option> <% }%> </select><br> </td> </tr> <tr> <td>Name:<font color="red">*</font></td><td><input type="text" name="name" id="name"value="" size="25"onkeypress="return ischarkey(event)"/></td> </tr> <tr> <td>Address:<font color="red">*</font></td><td><textarea name="address" id="address"rows="4" cols="20"onkeypress="return ischarkey(event)"> </textarea></td> </tr> <tr> <td>Pin code:</td><td> <input type="text" name="pin code" id="pin code"value="" size="7" onkeypress="return isNumberKey(event)"/></td> <td>Landline No:<font color="red">*</font></td><td><input type="text" name="landline" id="landline"value="" size="15" onkeypress="return isNumberKey(event)"/></td> <td>Fax No:</td><td><input type="text" name="fax" id="fax"value="" size="15" onkeypress="return isNumberKey(event)"/></td> </tr> <td>Mobile No:</td><td><input type="text" name="mobile" id="mobile"value="" size="15" onkeypress="return isNumberKey(event)"/></td> <td>Email 1:<font color="red">*</font></td><td><input type="text" name="email1" id="email1"value="" size="20"/></td> <td>Email 2:</td><td><input type="text" name="email2" id="email2"value="" size="20"/></td> <tr> </tr> <tr> <td colspan="6"><br> <center><input onclick="deleteRecord();" type="submit" value="Delete" name="insert" /> <input size="15" type="submit" value="Edit" name="insert" /> <a href="Admin_main.jsp"><input type="button" value="Cancel" name="cancel" /></a> </center><br> <%String msg = request.getParameter("msg"); if (msg != null) {%> <center><label><font color="red"><%=msg%></font></label></center> <% } %> </td> </tr> </table></center></font> <input type="hidden" name="username" value="<%= username%>" /> <input type="hidden" name="pass" value="<%= pass%>" /> </form>
Javascript code whcish was given by you
function deleteRecord(){ var doIt=confirm('Do you want to delete the record?'); if(doIt){ var f=document.myform; f.method="post"; f.action='../delete_sup?'; f.submit(); } else{ } }
Please help me, I m stuck. Thanks
Check this:
1)ajax.jsp:
<%@page import="java.sql.*"%> <html> <head> <script type="text/javascript"> function showData(id){ xmlHttp=GetXmlHttpObject() var url="getdata.jsp"; url=url+"?id="+id; xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged(){ if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ var showdata = xmlHttp.responseText; var strar = showdata.split(":"); document.getElementById("name").value= strar[1]; document.getElementById("address").value= strar[2]; document.getElementById("contactNo").value= strar[3]; document.getElementById("email").value= strar[4]; } } function GetXmlHttpObject(){ var xmlHttp=null; try { xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function confirmDelete(){ var doIt=confirm('Do you want to delete the record?'); if(doIt){ var f=document.employee; f.method="post"; f.action='deleteRecord.jsp'; f.submit(); } else{ } } </script> </head> <body> <form name="employee" > <br><br> <table > <div id="mydiv"></div> <tr><td><b>Select:</b></td><td><select name="id" onchange="showData(this.value);"> <option value="-1">--Select--</option> <% Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from employee "); while(rs.next()){ %> <option value="<%=rs.getInt("id")%>"><%=rs.getInt("id")%></option> <% } %> </select></td></tr> <tr><td><b>Name:</b></td><td><input type="text" name="name" id="name" ></td></tr> <tr><td ><b>Address:</b></td><td> <input type="text" name="address" id="address" ></td></tr> <tr><td><b>ContactNo:</b></td><td> <input type="text" name="contactNo" id="contactNo" ></td></tr> <tr><td><b>Email:</b></td><td> <input type="text" name="email" id="email" ></td></tr> </table> <input type="button" value="Delete" onclick="confirmDelete();"> </form> <table border="0" width="100%" align="center"> <br> <br> </table> </body> </html>
continue..
2)getdata.jsp:
<%@ page import="java.sql.*" %> <% int id = Integer.parseInt(request.getParameter("id")); String data =""; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from employee where id="+id+""); while(rs.next()) { data =":"+rs.getString("name")+":"+rs.getString("address") + ":" + Integer.parseInt(rs.getString("contactNo"))+":"+rs.getString("email"); } out.println(data); System.out.println(data); } catch (Exception e) { System.out.println(e); } %>
3)deleteRecord.jsp:
<%@ page import="java.sql.*" %> <% int id = Integer.parseInt(request.getParameter("id")); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); int i=st.executeUpdate("delete from employee where id="+id+""); out.println("Record is deleted successfully"); response.sendRedirect("ajax.jsp"); } catch (Exception e) { System.out.println(e); } %>
Ads