?
Solved

Delete multiple records using ajax/php/jquery

Posted on 2014-07-30
4
Medium Priority
?
1,993 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
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…
Suggested Courses

800 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