Solved

Delete multiple records using ajax/php/jquery

Posted on 2014-07-30
4
1,792 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

912 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

24 Experts available now in Live!

Get 1:1 Help Now