1

Good evening guys,

I'm trying to pass multiple checkbox values through AJAX and process them with a external .php script. Goal: delete multiple rows using checkboxes without refreshing the page.

Please assist me with passing the selected checkboxes into the datastring and putting them in the mysql command in the external .php file. This is the code so far:

The checkboxes:

<form name="frmMain" id="myForm" method="post" OnSubmit="return onDelete();">


<input class="checkbox_button_del" type="submit" id="buttondel" value="Delete" /> // submit to ajax


<input type="checkbox" class="cb-element" name="chkDel[]" id="chkDel<?=$i;?>" value="' .($id). '">


<input type="hidden" name="hdnCount" value="<?=$i;?>">
</form>

The AJAX:

$(function () {
    $(".checkbox_button_del").click(function () {
        var id = $(this).attr("id");
        var dataString = 'id=' + id; //pass checkbox ids somehow
        var parent = $(this).parents('tr:first');


        $.ajax({
            type: "POST",
            url: "core/actions/delete_multiple.php",
            data: dataString,
            cache: false,

            success: function () {
                parent.fadeOut('300', function () {
                    $(this).remove();
                });
                $("#display").load("display.php")

            }
        });

        return false;
    });
});   

The delete script:

// receive checkbox ids from ajax and delete rows

    for($i=0;$i<count($_POST["chkDel"]);$i++)
    {
        if($_POST["chkDel"][$i] != "")
        {
            $strSQL = "DELETE FROM players ";
            $strSQL .="WHERE id = '".$_POST["chkDel"][$i]."' ";
            $objQuery = mysql_query($strSQL);
        }
    }
thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67
Maarten Hartman
  • 1,611
  • 6
  • 26
  • 45
  • How is this substantially different from [this question](http://stackoverflow.com/questions/8973509/delete-multiple-entries-with-ajax/8974217) that you asked earlier? – Tim Post Jan 24 '12 at 10:24

2 Answers2

1

jQuery has this functionality built-in.

See: http://api.jquery.com/serialize/

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

You can do this by putting all the checkboxes in a form. And then add the following line in your function:

datastring = $('#myform').serialize();
bigblind
  • 12,539
  • 14
  • 68
  • 123