0

I have a little problem regarding with deleting multiple records in my table. I used checkbox in order to delete them but it doesn't work. I don't know what would be the exact CODE for it.

Here is my PHP code

<?php   
    echo "<form action='#'>
        <fieldset>
            <input type='text' name='search' value='' id='searchtalents' placeholder='Search Keywords..' size='40'/>
        </fieldset> 
    </form>";

    echo "<form name='tmsform' method='POST' action=''>";

    $sql = "SELECT * FROM talentinfo WHERE 1 LIMIT 10";
    $result = mysql_query($sql);
    if (!$result) {
        echo "Could not successfully run query ({$sql}) from DB: " . mysql_error();
        exit;
    }

    if (mysql_num_rows($result) == 0) {
        echo "No rows found";
        exit;
    }

    echo"<div id='talentinfo'><table id='mr_database' name='myform' style='border:1px solid #fff;' cellspacing=0 cellpading='2' class='pretty'>
        <thead>
            <tr>
                <th></th>
                <th></th>
                <th></th>
                <th>Mr Tracking Number</th>
                <th>First Name</th>
                <th>Middle Name</th>
                <th>Last Name</th>
                <th>Address</th>
                <th>Contact Number</th>
                <th>School</th>
                <th>Course</th>
                <th>Year Graduated</th>
                <th>Position Considered</th>
                <th>Referred Location</th>
                <th>Unit</th>
            </tr>
        </thead>";

    $counter = 40000;
    while ($row = mysql_fetch_assoc($result)) {
    $filled = (($counter % 2) == 1) ? "style='background-color:#BCD9E1;'" : "" ;
    $id = $row['talents_id'];

    echo "<tbody><tr {$filled} class='tmsdel'>";        
    echo "<td><a href ='#' rel='#edit_talents{$counter}'><center><img src='img/edit.gif' width='25' height='21' title='Edit'></center></a></td>";
    echo "<td><a href ='#'  id=".$row['talents_id'].'&idelete=talents'." class='delete'><center><img src='img/delete.png' width='25' height='21' title='Delete'></center></a></td>";
    echo "<td><input type='checkbox' name='checkbox[]' id='check".$row['talents_id']."' value='".$row['talents_id'].'&idelete=talents'."'/></td>";          
    echo "<td><a href='#' rel='#tracing_number{$counter}' style='text-decoration:none; font-weight:bold; color:#444;'>" . $row ['mr_tracking_number'] . "</a></td>";
    echo "<td>" . $row['firstname'] . "</td>";
    echo "<td>" . $row['middlename'] . "</td>";
    echo "<td>" . $row['lastname'] . "</td>";
    echo "<td>" . $row['address'] . "</td>";
    echo "<td>" . $row['contact_number'] . "</td>";
    echo "<td>" . $row['school'] . "</td>";
    echo "<td>" . $row['course'] . "</td>";
    echo "<td>" . $row['year_graduated'] . "</td>";
    echo "<td>" . $row['position_considered'] . "</td>";
    echo "<td>" . $row['referred_location'] . "</td>";
    echo "<td>" . $row['unit'] . "</td>";
    echo "</tr></tbody>";
?>

And here is my Javascript

$(function(){
    $(document).ready(function(){
    });     

    $("#delete-all").click(function(){
        $("#mr_database").remove();
        $.ajax({
            url: "modules/delete-all.php",
            type: "get",
            async: false,
            data: "check"
        });
    });
});
5
  • 2
    is this your query- ? $sql = "SELECT * FROM talentinfo WHERE 1 LIMIT 10"; ????? although this is going to work but i guess you wan tto save some condtn in WHERE Commented Nov 8, 2012 at 9:04
  • Rory McCrossan what kind of condition sir? Commented Nov 8, 2012 at 9:09
  • swapnesh like what condition sir?..I'm still getting to learn on PHP..thank you Commented Nov 8, 2012 at 9:10
  • first of all it would be g8 if u use only Swapnesh/@swapnesh :) and next if ur fetching all results then its ok if u use only this -> SELECT * FROM talentinfo Commented Nov 8, 2012 at 9:14
  • @swapnesh ok sir...i got it..what is the difference if I remove the WHERE 1 in my query? Thank you for the reply Commented Nov 8, 2012 at 9:20

2 Answers 2

1

Please follow steps:

  1. First you need to apply any class name to your all checkboxes.
  2. Call function on button click for delete then

      var allVals = '';
    
      $("#delete-all").click(function(){
    
       $('.checkboxclass :checked').each(function() {
         allVals = allVals + $(this).val() + ',';
       });
    }
    
  3. Then you need to pass allVals variables in ajax and post to .php file

    Like: data: 'ids=' + allVals in $.ajax

  4. In last you can get this variable in php file and do delete process on it.

    Like: $ids = explode(',', $_REQUEST['ids'); and use ids in mysql query

    Hope this helps you.

Sign up to request clarification or add additional context in comments.

2 Comments

var allVal = 'checkboxes'; $("#delete-all").click(function(){ $('.checkboxes :checked').each(function(){ allVal = allVal + $(this).val() + ','; }); Check = confirm("Do you really want to delete ALL DATA ? (Hint: there is no undo)"); if (Check == true) { $.ajax({ url: "modules/delete-all.php", type: "post", async: false, data: "talents_id" + allVal }); } });
You can put this code which i change in your above code $("#delete-all").click(function(){var allVal = ''; $('.checkboxes :checked').each(function(){ allVal = allVal + $(this).val() + ','; }); Check = confirm("Do you really want to delete ALL DATA ? (Hint: there is no undo)"); if (Check == true) { $.ajax({ url: "modules/delete-all.php", type: "post", async: false, data: "talents_id=" + allVal }); } }); ("checkboxes" is a classs name which you need to assign in all checkboxes)
0

Rather simple answer since what i am seeing here is a script to view the table and delete the html in the browser but not the data on the server.

You need to write the modules/delete-all.php script mentioned here: url: "modules/delete-all.php"

which should contain the " delete * FROM talentinfo " SQL Query.

This however will seamlessly delete all table contents. So you might want to do a prompt:

$("#delete-all").click(function(){

Check = confirm("Do you really want to delete ALL DATA ? (Hint: there is no undo)");
if (Check == true) {
 $("#mr_database").remove();
    $.ajax({
        url: "modules/delete-all.php",
        type: "get",
        async: false,
        data: "check"
    });

}
});

The delete_all.php should delete all, so just do:

<?php   

$sql = "delete * FROM talentinfo ";
$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ({$sql}) from DB: " . mysql_error();
    exit;
} else {
    print "ok";
}
?>

6 Comments

include("../includes/initialize.php"); $id = isset($_GET['id']) ? $_GET['id'] : null; switch($id){ case "talents_id" $sql = "DELETE from talentinfo WHERE talents_id= $id"; break; } on what part will I put the Check = confirm("Do you really want to delete ALL DATA ? (Hint: there is no undo)"); if (Check == true) { // do the deleting here }
I changed my answer to indicate the position of the confirm().
concerning your delete_all.php: you do not pass any parameters in the $.ajax** call so you wont get any **$_GET['id'] in the delete_all.php. You also call the delete-all.php from the delete all button. You do not need a parameter to delete all.
Hi thank you for your answer...my little problem is that it doesn't really delete the table. When I refresh the site it was there again.
Hi thank you for your answer...my little problem is that it doesn't really delete the table. When I refresh the site it was there again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.