0

First of all, I would like to apologize if there is any duplicate question asked.

As the title says, I am trying to delete multiple rows that are checked by the user with the help of jQuery. Unfortunately, I am failing to achieve this task.

Here's the code that I am using:==>

index.php --> Displaying the data in the tabular format..

<?php
require_once '../../Classes/class.Validation.php';
$validate = new Validation('ecommerce');
$qu = "SELECT ProdCode, ProdName, ProdDescription, ProdRate, ProdTotalQuantity, ProdAvailability FROM products";
$validate->Query($qu);
if ($validate->NumRows()) {
?>
    <div class="container-fluid">
        <div class="table-responsive">
            <table border="1" class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="all" name="deletePrd[]"></th>
                        <th>Prod-Image</th>
                        <th>Prod-Code</th>
                        <th>Prod-Name</th>
                        <th>Prod-Description</th>
                        <th>Prod-Rate</th>
                        <th>Prod-Quantity</th>
                        <th>Prod-Availability</th>
                        <th>Action</th>
                    </tr>
                    <tr>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>
                    <th>
                        <input class="filter" name="Pro-Code" placeholder="Prod-Code" data-col="Prod-Code">
                    </th>
                    <th>
                        <input class="filter" name="Prod-Name" placeholder="Prod-Name" data-col="Prod-Name">
                    </th>
                    <th>
                        <input class="filter" name="Prod-Description" placeholder="Prod-Description" data-col="Prod-Description">
                    </th>
                    <th>
                        <input class="filter" name="Prod-Rate" placeholder="Prod-Rate" data-col="Prod-Rate">
                    </th>
                    <th>
                        <input class="filter" name="Prod-Total-Quantity" placeholder="Prod-Quantity" data-col="Prod-Quantity">
                    </th>
                    <th>
                        <input class="filter" name="Prod-Availability" placeholder="Prod-Availability" data-col="Prod-Availability">
                    </th>
                        <th>&nbsp;</th>
                    </tr>
                    </thead>
                    <tbody>                 
                        <?php
                        while ( $row = $validate->FetchAllDatas() ) {
                            echo '<tr>
                            <td><input type="checkbox" id="'.$row["ProdCode"].'" name="deletePrd[]"></td>
                            <td>
                                <img src="http://www.example.com/ECommerce/Images/Products/'
                                .$row["ProdCode"].'.jpg" alt="'.$row["ProdName"].'"
                                width="75" height="50" />
                            </td>           
                            <td>' . $row["ProdCode"] . '</td>
                            <td>' . $row["ProdName"] . '</td>
                            <td>' . $row["ProdDescription"] . '</td>
                            <td>' . $row["ProdRate"] . '</td>
                            <td>' . $row["ProdTotalQuantity"] . '</td>
                            <td>' . $row["ProdAvailability"] . '</td>
                            <td>
                                <a href="http://www.example.com/ECommerce/Administrators/Products/UpdateProduct.php?prd='.$row["ProdCode"].'">UPDATE</a><br /><a href="http://www.example.com/ECommerce/ActionFiles/DeleteProduct.php?prd='.$row["ProdCode"].'">DELETE</a>
                            </td></tr>';
                        }
                        ?>                  
                    </tbody>
                </table>
            </div>
        </div>
        <?php
    } else {
        echo '<p class="lead">No Products. Start inserting by clicking <a href="http://www.example.com/ECommerce/Administrators/Products/AddProducts.php">here</a></p>';
    }
    ?>

Here's the link that I am using on click of the button:==>

<a class="btn btn-link" id="del" name="DeleteLink">DELETE</a>

Here's the jQuery code to delete the product:==>

$("a[id='del']").click(function() {
        var count_checked = $("[name='deletePrd[]']:checked").length;
        var checkedCode = $("[name='deletePrd[]']:checked").attr("id");
        if(count_checked == 0) {
            alert("Please select product(s) to delete.");
            return false;
        }
        if(count_checked == 1) {
            //return confirm("Are you sure you want to delete this product ?");
            if (confirm('Are you sure you want to delete this product ?')) {
                $("a[id='del']").attr("href", "http://www.example.com/ECommerce/ActionFiles/DeleteProduct.php?prdCode=" + checkedCode);
            }
        } else if(count_checked > 1) {
            if (confirm('Are you sure you want to delete the selected products ?')) {
                $("a[id='del']").attr("href", "http://www.example.com/ECommerce/ActionFiles/DeletedSelectedProducts.php");
                for (var i = 0; i < count_checked; i++) {
                    console.log(i);
                }
            }           
        }
    });

Here's the php code to delete the product:==>

DeletedSelectedProducts.php:

<?php
session_start();

require_once '../Classes/class.Validation.php';
$validate = new Validation('developi_ecommerce');

if (isset($_POST['DeleteLink']) && isset($_POST['deletePrd'])) {
    $delete = $_POST['deletePrd'];

    $res = "";

    foreach ($delete as $code) {
        $q = "DELETE FROM products WHERE ProdCode = '".$code."' ";
        $res = $validate->Query($q);
    }
    if (!$res) {
        echo '<br />Not Deleted';
    } else {
        echo '<br />Deleted';
    }
} else {
    echo 'Product Not Set';
}

if ($validate->IsConnected()) {
    $validate->DisConnect();
}

I tried, and I get 'Product not set' as the output.

I am pretty sure that somewhere I have made a mistake, but I am unable to detect it. Kindly help me out and also try helping me to rectify it.

Thank You in advance.

P.S. I have tried and tested a single product deletion and all product deletion and it works perfectly without any errors.

Update 1

Here's the updated jQuery with AJAX:==>

$("a[id='del']").click(function() {
        var count_checked = $("[name='deletePrd[]']:checked").length;
        var checkedCode = $("[name='deletePrd[]']:checked").attr("id");
        if(count_checked == 0) {
            alert("Please select product(s) to delete.");
            return false;
        }
        if(count_checked == 1) {
            //return confirm("Are you sure you want to delete this product ?");
            if (confirm('Are you sure you want to delete this product ?')) {
                $("a[id='del']").attr("href", "http://www.example.com/ECommerce/ActionFiles/DeleteProduct.php?prdCode=" + checkedCode);
            }
        } else if(count_checked > 1) {
            if (confirm('Are you sure you want to delete the selected products ?')) {
                /*$("a[id='del']").attr("href", "http://www.developigner.com/ECommerce/ActionFiles/DeletedSelectedProducts.php");
                for (var i = 0; i < count_checked; i++) {
                    console.log(i);
                }*/
                $("a[id='del']").
                attr("href", "http://www.developigner.com/ECommerce/ActionFiles/DeletedSelectedProducts.php").
                ajax({
                    type: "post",
                    data: count_checked.serialize(),
                    success: function() {
                        alert(count_checked);
                    }
                });
            }           
        }
    });
Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71
  • without form, either you post data using ajax onClick or pass mulitple selected ids by comma separated as query parameter on URL – GBD Nov 22 '14 at 06:46
  • How do I do that ? the thing is that I am a beginner in this – Saiyan Prince Nov 22 '14 at 06:48
  • Please this: http://stackoverflow.com/questions/20543722/ajax-post-within-jquery-onclick – GBD Nov 22 '14 at 06:52
  • Please also see this: http://stackoverflow.com/questions/12433438/how-to-send-the-values-of-an-array-of-checkboxes-through-ajax-using-jquery – GBD Nov 22 '14 at 06:54
  • @GBD I am gettitng NULL as output when used the answer provided in the second link.. Check the updated jQuery part now.. – Saiyan Prince Nov 22 '14 at 07:26

1 Answers1

0

Replace

data: count_checked.serialize(),

With

data: $("[name='deletePrd[]']:checked").serialize(),
GBD
  • 15,847
  • 2
  • 46
  • 50