使用jquery和ajax删除记录

时间:2016-05-04 05:32:44

标签: javascript jquery ajax codeigniter

当我使用此代码时,它会成功进入其他条件并显示错误。但是当我刷新页面时,记录会自动删除。 请建议我能做些什么。

jquery脚本:

// Client
...
connect(socket);
...
write(socket, order, sizeof(order));
read(socket, result, sizeof(result));
// repeat for other orders, as required by client...


// server
...
socket = accept(); // happens for each client
...
pthread_create(runner);
...

// server thread (runner)
...
for(;;)
{
  int r(0);
  for(;;)
  {
    r += read(socket, order, sizeof(order));
    if(r >= sizeof(order))
    {
      break;
    }
    // wait for more data is not enough received yet
    poll(..."socket" + "eventfd"...);  // <-- this will often take 40ms
    if(eventfd_happened)
    {
      // quit thread
      return;
    }
  }
  ...
  [work on order]
  ...
  write(socket, result, sizeof(result));
}

查看文件:

<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> 

控制器文件:

<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>

5 个答案:

答案 0 :(得分:0)

只需更改控制器功能的最后一行:

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

echo 'Success';

答案 1 :(得分:0)

将控制器功能更新为,

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

答案 2 :(得分:0)

我刚刚在脚本中为ajax尝试了以下条件,它确实有效。我想我没有确认或没有回应,所以这就是为什么它会继续进入写入错误的其他条件。

希望这会对某人有所帮助。

我的新剧本

<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> 

答案 3 :(得分:0)

你的ajax返回一个对象,所以你不能像这样使用if (response == "Success")。 改变这样的代码:

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;
}

如果它是真的它会返回1,所以chage ajax就像这样:

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

如果回复为真,我认为你想要隐藏tr

答案 4 :(得分:0)

因为您将其作为数组传递(在javascript中它将被转换为对象),响应的值将以<appSettings> 的形式提供。如果将response.success对象写入控制台,则可以检查它。

ajax方法

中尝试此操作
response

修改 我建议你改变控制器方法的输出:

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

    }

而不是

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