Solved

Delete multiple records using ajax/php/jquery

Posted on 2014-07-30
4
1,806 Views
Last Modified: 2014-08-01
I need to be able to delete multiple records using checkboxes, ajax and php.

Here is the checkbox line in a form called -  CourseEditForm: (the form does not have a php file assigned in its action field. It is supplied during post in the js file below)

<input type="checkbox" value="<?php echo $courseID; ?>" name="groupDelete[]" id="groupDelete" />

Here is the button to submit the form: (it is an image button)
<input type="image" src="deletebutton.jpg" id="submitGroupDelete" />

And here is the js code:

$(document).ready(function(){
$("#submitGroupDelete").click( function(e) { 

	 e.preventDefault();

	var count_checked = $("[name='groupDelete[]']:checked").length; // count the checked
	if(count_checked == 0) {
	      alert("Please select a course to delete.");
	      return false;
	}
	if(count_checked == 1) 
	{
	       return confirm("Are you sure you want to delete this course?");
        } 
       else 
       {
           return confirm("Are you sure you want to delete these courses?");


	$.post ( "groupDelete.php", $("#CourseEditForm :input").serializeArray(), function(info) {
				
				$("#CoursesTable").load("CourseList.htm"); //These are divs where info is displayed
				$("#editCourse").html(info);
						
				});
		}

});

Open in new window


When I click the button is goes as far as the confirmation message - Are you sure you want to delete these courses, and then nothing happens. There are at least two things that I am  not sure of: the info in function(info) and whether return confirm is supposed to return a yes or no value to be used for further processing. I am new to ajax.

Your prompt response is appreciated.
0
Comment
Question by:davidgm
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40230132
Change your code slightly

$(document).ready(function () {
    $("#submitGroupDelete").click(function (e) {

        e.preventDefault();

        var count_checked = $("[name='groupDelete[]']:checked").length; // count the checked
        if (count_checked == 0) {
            alert("Please select a course to delete.");
        }
        if (count_checked == 1) {
            // left this as is since I don't what it's supposed to do
            return confirm("Are you sure you want to delete this course?");
        } else {
            if (confirm("Are you sure you want to delete these courses?")) {
                $.post("groupDelete.php", $("#CourseEditForm :input").serializeArray(), function (info) {
                    $("#CoursesTable").load("CourseList.htm"); //These are divs where info is displayed
                    $("#editCourse").html(info);
                });
            }
        }
    });
});

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40230143
Actually you have logic issues

        if (count_checked == 0) {
            alert("Please select a course to delete.");
        }
        if (count_checked == 1) {
            // left this as is since I don't what it's supposed to do
            return confirm("Are you sure you want to delete this course?");
        } else { // this is the same as count_checked == 0
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40234583
greetings davidgm, , your AJAX code for jquery post is NOT correct, the jquery post API help is here -
http://api.jquery.com/jQuery.post/

in a POST you HAVE to have a URL string OR a javascript Object in the "data" section of your post object, you try to use an Array, which will ERROR out. you might try this -
$.post ( "groupDelete.php", $("#CourseEditForm").serialize(), function(info) {
    $("#CoursesTable").load("CourseList.htm"); //These are divs where info is displayed
    $("#editCourse").html(info);
    });

Open in new window



here is one example they give-
$.post( "test.php", $( "#testform" ).serialize())
.done(function( data ) {
alert( "Data Loaded: " + data );
});
ask questions if you need more info
0
 

Author Closing Comment

by:davidgm
ID: 40234834
Good solution. The main problem was with the confirm.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now