0

i am trying to delete values with check box using ajax call can any one help me out.

unable to find the error and one nore thing this is a template and i hav a feature of check all inbuilt so do need to change anyinbuilt code for check box This is my listing form:

<form id="stafflistForm">
<input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>">
<button id="deleteChecked"><i class="fa fa-trash-o"></i></button>
</form>

This my Ajax Script:

<script language="JavaScript">  
    $("#deleteChecked").click(function()
    {
        $("#delshowMessageDiv").hide();
        $("#delshowMessage").html('');
        $.ajax({
            url: "staffcontroller.php",
            method: "POST",
            data: { delData : $("#stafflistForm").serialize(), 'action':'delete'},
            dataType: "json",
            success: function (response) {
                if(response["success"]==true)
                {
                    $("#delshowMessageDiv").hide();
                    $("#delshowSuccessMessageDiv").show();
                    $("#delshowSuccessMessage").html(response["message"]);
                    }else{
                    $("#delshowMessageDiv").show();
                    $("#delshowMessage").html(response["message"]);
                }   
            },
            error: function (request, status, error) {
                $("#hshowMessageDiv").show();
                $("#hshowMessage").html("OOPS! Something Went Wrong Please Try After Sometime!");
            }
        });
        return false;
    });     
</script>

And this is my controller page:

else if($_REQUEST['action']=='delete'){
    $delids=explode(",",$_REQUEST["checkedids"]);
    $count=count($delids);
    for($i=0;$i<$count;$i++)
    {
        $delQuery= $conn->query("DELETE FROM os_staff WHERE id_staff=".$delids[$i]);
    }
        if($delQuery){
        $response['message'] = "<strong>Success!</strong>Staff Deleted Successfully.";
        $response['success'] = true;
        }else{
        $response['message'] = "<strong>Warning!</strong> Staff Not Deleted.Please Check Carefully..";
        $response['success'] = false;
    }
    echo json_encode($response);
    exit;
}
2
  • 1
    Dont use html attribute id if you using it multipli id="deleteChecked". Use class selector or data attribute instead. That should be help to solve your issue. Commented Oct 18, 2016 at 13:28
  • You can use the WHERE IN clause in SQL: $delQuery= $conn->query("DELETE FROM os_staff WHERE staff IN ('9','7','6')); Just change the value of your formats into this format ('9','7','6') you can use implode() Commented Oct 18, 2016 at 13:34

2 Answers 2

1

First of all: Dont use html attribute id if you using it multipli id="deleteChecked". Use class selector or data attribute instead.

Here is a small script which show you how you can improve your code. That should be help you to solve your issue.

$(document).ready(function() {
  
  $('.delete-user').on('click', function(e) {
    // do here your ajax
    
    // this is just example
    $(this).parents('tr').remove();
    
  });
  
});
.fa-trash-o {
  padding: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <form class="stafflistForm">
        <input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>">
        <button class="delete-user"><i class="fa fa-trash-o"></i></button>
      </form>
    </td>
    <td>Tom</td>
  </tr>  
  <tr>
    <td>
      <form class="stafflistForm">
        <input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>">
        <button class="delete-user"><i class="fa fa-trash-o"></i></button>
      </form>
    </td>
    <td>Peter</td>
  </tr> 
  <tr>
    <td>
      <form class="stafflistForm">
        <input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>">
        <button class="delete-user"><i class="fa fa-trash-o"></i></button>
      </form>
    </td>
    <td>Son Goku</td>
  </tr> 
  <tr>
    <td>
      <form class="stafflistForm">
        <input type="hidden" name="checkedids" value="<?php echo $staffResults['id_staff']; ?>">
        <button class="delete-user"><i class="fa fa-trash-o"></i></button>
      </form>
    </td>
    <td>Gozilla</td>
  </tr>   
</table>

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

1 Comment

He can also use $("button#deleteChecked").click(function()
1

The PHP script should set the correct mime type via:

header('Content-type: application/json');

Besides that: Why do you have separate DIV containers for the error and success messages? Why not have one "feedback" div, which gets a CSS class which does the formating (based on error or success).

Comments

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.