File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Deleting multiple records using jQuery-ajax at a time. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Deleting multiple records using jQuery-ajax at a time." Watch "Deleting multiple records using jQuery-ajax at a time." New topic
Author

Deleting multiple records using jQuery-ajax at a time.

kaustubh kurambhatti
Greenhorn

Joined: Oct 21, 2009
Posts: 13
Using jQuery-ajax, i am trying to develop an application. I want to delete districts from database table named district_details. I am showing districts in tabular format as an user interface and provided a checkbox against each and every district. On click of Delete button i want to delete selected districts from table. But at a time, i can delete only 1 district from database. Multiple districts can not be deleted. I am providing with the code snippet. so please help.

modifydb.jsp

<script type="text/javascript">
$(document).ready(function(){
$('#myDiv1').load('http://localhost:8080/statsdept/admin/showDistrict.jsp');// showing the //districts
$("#b01").click(function(){
$('#myDiv2').load('http://localhost:8080/statsdept/addDistrict', //for adding district
{ name: $("#district").val()});
$('#myDiv1').load('http://localhost:8080/statsdept/admin/showDistrict.jsp');
});

$("#Delete").click(function(){ // delete districts
var selectedItems = new Array();
$("input[@name='courseCb[]']:checked").each(function() {selectedItems.push($(this).val());});
alert(selectedItems)
if (selectedItems .length == 0)
alert("Please select item(s) to delete.");
else
$.ajax({
type: "POST",
url: "http://localhost:8080/statsdept/delDistrict",
data: "items=" + selectedItems,
dataType: "text",
success: function (request) {
$('#myDiv1').load('http://localhost:8080/statsdept/admin/showDistrict.jsp');

},
error: function(request,error){
alert('Error deleting item(s), try again later.');
}
})
});//end of trial button clicked function.


});//end of document.ready function.


</script>

<p class="msg_head">District Info</p>

<div class="msg_body">

<div id="myDiv1"></div>
<button id="Delete" type="button">Delete District</button><br>
District Name : <input type="text" id="district"><br>
<button id="b01" type="button">Add District</button>
<div id="myDiv2"></div>

</div>


delDistrict.java

try {
String[] sr_nos = request.getParameterValues("items"); //value from checkbox
//String[] sr_nos = {"32","35"}; //value from checkbox
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet delDistrict</title>");
out.println("</head>");
out.println("<body>");

dbCon dc = new dbCon();
Connection con = dc.getConnection1();

try{
Statement stmt = con.createStatement();
int arrayElement = 0;
for(int i = 0; i<sr_nos.length; i++){
//out.println("<script>alert(" + sr_nos[i] + ")</script>");
arrayElement = Integer.parseInt(sr_nos[i]);
stmt.executeUpdate("delete from district_details where dst_id = " + arrayElement);
}
}
catch(Exception e){
out.println(e);
}

out.println("</body>");
out.println("</html>");

I am new to jQuery..... please help ... thanks
Hardik N Shah
Greenhorn

Joined: Jul 14, 2010
Posts: 7
use $.post() for jquery ajax and pass your selected value as parameter of ajax call

http://api.jquery.com/jQuery.post/
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

are you getting array at server side?


Hardik Shah
Java/J2ee Expert
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Deleting multiple records using jQuery-ajax at a time.
 
Similar Threads
jQuery: how to determine if any checkbox is checked.
Display / Hide Table based on output
Filter on name in jQuery
toggle checkbox group when the max limit is reached
Can you post data from textfields to a URL without having a <form>