Solved

Delete multiple records using ajax/php/jquery

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

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

808 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