简体   繁体   English

在Codeigniter中使用Ajax更新数据库记录

[英]Update Database Record with Ajax in Codeigniter

I am trying to update database records using ajax from the ajax response, getting success message but the actual database records are not updated at all. 我正在尝试从ajax响应中使用ajax更新数据库记录,并获得成功消息,但实际的数据库记录根本没有更新。 But it wonder how the ajax response should throw the success message while the query is not updating the database. 但是它想知道,当查询不更新数据库时,ajax响应应如何抛出成功消息。

VIEW: 视图:

 // AJAX code to update the database // update marks when form is submitted $('#updateMarks').on('submit',function(event) { event.preventDefault(); var practical_mark = $("#mark_written").val(); var written_mark = $("#mark_practical").val(); var comment = $("#comment").val(); var mark_id = $("#mark_id").val(); $.ajax({ type: "POST", url: "<?php echo site_url('admin/exam_marks_update'); ?>", data: { practical_mark : practical_mark, written_mark: written_mark, comment : comment, mark_id : mark_id }, success: function(response) { alert("success"); }, error: function(){ alert("Error"); }, }); }); 
 <?php foreach($marks as $row2): ?> <form method="post" role="form" id="updateMarks"> <tr> <td class="text-center"><?php echo $student['name']; ?></td> <td> <!-- create two col table for marks category --> <table class="table table-bordered table-hover toggle-circle"> <thead> <tr> <th data-toggle="true" class="text-center"><?php echo get_phrase('written_exam'); ?></th> <th data-toggle="true" class="text-center"><?php echo get_phrase('practical_exam'); echo get_phrase('_(out_of_100)'); ?></th> </tr> </thead> <tbody> <tr> <td class="text-center"><input type="number" value="<?php echo $row2['written_mark_obtained'];?>" id="mark_written" name="mark_written" class="form-control" /></td> <td class="text-center"><input type="number" value="<?php echo $row2['practical_mark_obtained'];?>" id="mark_practical" name="mark_practical" class="form-control"/></td> </tr> </tbody> </table> <!-- end create two col table for marks category --> </td> <td class="text-center"><textarea class="form_control" id="comment" name="comment" rows="4" > <?php echo $row2['comment'] ?> </textarea></td> <td class="text-center"> <input type="hidden" id="mark_id" name="mark_id" value="<?php echo $row2['mark_id'];?>" /> <button type="submit" class="btn btn-block btn-success btn-md"><i class="icon pe-pen" aria-hidden="true"></i><?php echo get_phrase('update'); ?></button> </td> </tr> </form> <?php endforeach; ?> 

Controller: 控制器:

 function exam_marks_update(){ $data['written_mark_obtained'] = $this->input->post('written_mark'); $data['practical_mark_obtained'] = $this->input->post('practical_mark'); $data['comment'] = $this->input->post('comment'); $this->crud_model->update_student_marks($data, $this->input->post('mark_id')); } 

MODEL 模型

 function update_student_marks($data, $mark_id){ $this->db->where('mark_id', $mark_id); $this->db->update('mark', $data); } 

Your Controller retrieving inputs which doesn't exists... you need to pass your name, id as inputs and not the value which you echo... see as Controller: 您的控制器正在检索不存在的输入...您需要传递name, id作为输入,而不是您要回显的值...请参见控制器:

function exam_marks_update(){
$data = array(
    'written_mark_obtained'   => $this->input->post('written_mark'),
    'practical_mark_obtained' => $this->input->post('practical_mark'),
    'comment'                 => $this->input->post('comment')
);
$this->db->where('mark_id', $this->input->post('mark_id'));
$this->db->update('mark', $data);
}

and change this: 并更改此:

  var comment        = $("#comment").val();

to

  var comment        = $("#comment").html();

As comment is textarea... 由于评论是textarea ...

Jquery ajax success callback function is always called if the request to server succeeds. 如果对服务器的请求成功,则始终调用jQuery ajax success回调函数。 You need to return response data from server to verify when database operation was successful or not. 您需要从服务器返回响应数据以验证数据库操作何时成功。 I have edited your code , this might work for you. 我已经编辑了您的代码,这可能对您有用。

MODEL 模型

 function update_student_marks($data, $mark_id){
      .....
      return $this->db->update('mark', $data);
    }

Controller:: 控制器::

  function exam_marks_update(){
      .....
      if($this->crud_model->update_student_marks($data, $this->input->post('mark_id'))){
       echo json_encode(array('success' => true));
       exit;
     } else {
      echo json_encode(array('success' => false));
      exit;
    }
 }

View 视图

 $.ajax({
        type: "POST",
        url: "<?php echo site_url('admin/exam_marks_update'); ?>",
        dataType :'json',
        data: { practical_mark : practical_mark, 
               written_mark: written_mark, 
               comment : comment,
               mark_id : mark_id
              },
        success: function(response)
        {
          if (response.success === true){
            alert("success");
          } else {
            alert('failed');
          }
        },
        error: function(){
          alert("Error");
        },
      });

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

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