简体   繁体   English

使用jquery和ajax删除记录

[英]Delete record using jquery and ajax

When I am using this code it goes in success else condition and show error. 当我使用此代码时,它会成功进入其他条件并显示错误。 But when I refresh the page the record is deleted automatically. 但是当我刷新页面时,记录会自动删除。 Please suggest me what I can do. 请建议我能做什么。

jquery script: jquery脚本:

<script>
$(document).ready(function(){

$(".delete").click(function(event){
 alert("Delete?");
 var href = $(this).attr("href")
 var btn = $(this);

  $.ajax({
    type: "GET",
    url: href,
    success: function(response) {

      if (response == "Success")
      {
        $(btn).closest('tr').fadeOut("slow");
      }
      else
      {
        alert("Error");
      }

    }
  });
 event.preventDefault();
 })
 });
</script> 

View file: 查看文件:

<table style="width:100%" border="1" class="table">
    <thead>
    <tr>
            <th>score Id</th>
            <th>student name </th>
            <th>subject Name</th>
            <th>student marks</th>      
            <th>Action</th>
        </tr>
    </thead>    
        <?php foreach($score as $r): ?>
    <tbody> 
        <tr><td><?php echo $r->score_id; ?></td>
            <td><?php echo $r->student_name; ?></td>
            <td><?php echo $r->subject_name; ?></td>
            <td><?php echo $r->marks; ?></td>
            <?php if($status <> 1): ?>
            <?php if($admin_id == $r->admin_id): ?>
            <td><a class="btn btn-info" href="<?php echo base_url(); ?>index.php/score_listing/edit/<?php echo $r->score_id; ?>" > Edit</a><a class="btn delete btn-danger" href="<?php echo base_url(); ?>index.php/score_listing/delete/<?php echo $r->score_id; ?>"> Delete</a></td>
            <?php endif; ?>
            <?php else: ?>
            <td><a class="btn btn-info" href="<?php echo base_url(); ?>index.php/score_listing/edit/<?php echo $r->score_id; ?>" > Edit</a><a class="btn delete btn-danger" href="<?php echo base_url(); ?>index.php/score_listing/delete/<?php echo $r->score_id; ?>" > Delete</a></td>

            <?php endif; ?>         
        </tr>
    </tbody>    
        <?php endforeach; ?>

    </table>

Controller file: 控制器文件:

public function delete($id)
{
    $admin_name = $this->session->userdata('admin_name');  
    log_message('debug',"this record deleted by Admin = ".$admin_name );

    $score = $this->score->delete_operation($id);

    $error_data = array('error' => 'Error while deleting record');;
    return json_encode(array("success" => true));
}

Just change your controller function's last line: 只需更改控制器功能的最后一行:

return json_encode(array("success" => true));

to

echo 'Success';

update you controller function as, 将控制器功能更新为,

public function delete($id) {
...
...
die('Success');
}

I just tried the below condition in script for ajax and it works. 我只是在脚本中为ajax尝试了以下条件,它的工作原理。 I think I am not confirming or not taking response so thats why it continuously going into else condition where error is written. 我想我没有确认或没有回应,所以这就是为什么它会继续进入写入错误的其他条件。

Hope this will help some one. 希望这对一些人有所帮助。

MY new script 我的新剧本

<script>
$(document).ready(function(){

$(".delete").click(function(event){
 var del = confirm("Delete record?");
 var href = $(this).attr("href")
 var btn = $(this);
 if(del == true)
 {
  $.ajax({
    type: "GET",
    url: href,
    success: function(response) {

      if (response != "Success")
      {
        $(btn).closest('tr').fadeOut("slow");
      }
      else
      {
        alert("Error");
      }

    }
  });
}
 event.preventDefault();
})
});
</script> 

Your ajax return an object so you cant use if (response == "Success") like this. 你的ajax返回一个对象,所以你不能使用if (response == "Success")这样的。 change code like this: 像这样改变代码:

public function delete($id)
{
    $admin_name = $this->session->userdata('admin_name');  
    log_message('debug',"this record deleted by Admin = ".$admin_name );

    $score = $this->score->delete_operation($id);

    $error_data = 2;
    echo 1;
}

if it's true it return 1, so chage ajax like this: 如果它是真的它返回1,所以chage ajax像这样:

    if (parseInt(response)==1)
          {
            $(btn).closest('tr').fadeOut("slow");
          }
          else
          {
            alert("Error");
          }
    }

I assumed you want hide tr if response is true. 如果响应是真的,我假设你想要隐藏tr

Because you passed it as an array (in javascript it will be converted as an object), the value of the response will be available as response.success . 因为您将它作为数组传递(在javascript中它将被转换为对象),响应的值将作为response.success You can check the response object if you write it to the console. 如果将response对象写入控制台,则可以检查response对象。

Try this in your ajax method : 在你的ajax方法中尝试这个:

success: function(response) {
      console.log(response);
      if (response.success == true)
      {
        $(btn).closest('tr').fadeOut("slow");
      }
      else
      {
        alert("Error");
      }

    }

EDIT: And I suggest you to change the output in your controller's method : 编辑:我建议你改变控制器方法的输出:

$this->output->set_content_type('application/json')->set_output(json_encode(array("result" => true)));

instead of 代替

return json_encode(array("success" => true));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM