Solved

Delete multiple records using ajax/php/jquery

Posted on 2014-07-30
4
1,762 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Good solution. The main problem was with the confirm.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…

762 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

8 Experts available now in Live!

Get 1:1 Help Now