1

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. But it wonder how the ajax response should throw the success message while the query is not updating the database.

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);
}
sujan dahal
  • 41
  • 2
  • 10

2 Answers2

0

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:

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

Ilanus
  • 6,690
  • 5
  • 13
  • 37
  • You mean i have to set the parameter to the controller function ?? – sujan dahal Jan 20 '16 at 18:06
  • @sujandahal i meant u wrote the wront input names.. just try my controller it will work – Ilanus Jan 20 '16 at 18:09
  • I am trying your controller but no good luck for me :( any other solutions ?? i am also set my form inside foreach loop which is the reason for this issue ?? because the update button is just working for the first row retrieved through foreach – sujan dahal Jan 20 '16 at 18:19
  • so how can i pass the input values to ajax and ajax to controller. if you can mention it clearly with code then it will great help for me – sujan dahal Jan 20 '16 at 19:05
  • @sujandahal yes without the model just do as my code now – Ilanus Jan 20 '16 at 19:09
  • which shows the success message but unfortunately the db velues remains the same .. it doesn't changed .. Is my ajax function has any code that doesn't send the data array to controller – sujan dahal Jan 20 '16 at 19:22
  • @sujandahal when html is loaded open up the source code and check "", does it print the correct path to the ajax?? becuase the code is just fine – Ilanus Jan 20 '16 at 19:28
  • @sujandahal and alsot see my edit. use .html() on textarea not .val() – Ilanus Jan 20 '16 at 19:29
  • my actual path defines by ? means my URL looks like `http://localhost/vidya/index.php?admin/exam_marks` but using `` displays the `http://localhost/vidya/index.php/admin/exam_marks` and when i paste this on my URL bar it redirects me to dashboard as my default controller which is loaded if the user is logged in .. – sujan dahal Jan 20 '16 at 19:48
0

Jquery ajax success callback function is always called if the request to server succeeds. 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");
        },
      });
N Nem
  • 743
  • 8
  • 14