0

I am trying to update multiple data through AJAX but I have tried several times, but the data has not changed, please help to fix it in order to run perfectly.

Controllers:

function update_notif() {
    $this->My_model->update_multiple();
    $this->session->set_flashdata('msg', 
                '<div class="callout callout-warning">
                    <h4>Success</h4>
                </div>');                   
}

Models:

function update_multiple() {
    $update = $this->input->post('id_pesan');
    for ($i=0; $i < count($update) ; $i++) { 
        $data = array('notif' => 0);            
        $this->db->where('id', $delete[$i]);
        $this->db->update('my_table', $data);               
    }
}   

Views:

<button type="button" id="btn_delete" ></button>

<table class="table table-hover table-striped">
    <thead>
        <tr>
            <td><input id="selecctall" type="checkbox"></td>
            <td>No</td>
            <td>Name</td>
            <td>Telp</td>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($show_data as $row) { ?>
        <tr>
            <td><input type="checkbox" class="select_update" name="select_update[]" value="<?php echo $row->id; ?>"></td>
            <td><?php echo $row->no; ?></td>
            <td><?php echo $row->nama; ?></td>
            <td><?php echo $row->telp; ?></td>
        </tr>   
    </tbody>
    <?php } } ?>
</table>    

Ajax:

var getVar = [];  
$(".select_update:checked").each(function() {  
        getVar.push($(this).val());
}); 

$.ajax({
    url : url,
    type: 'POST',
    data: 'id_pesan='+getVar,
    dataType: 'json',
    success: function(response)
    {
        if(response){
            location.reload();
        }           
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Error');
    }
 });

2 Answers 2

2

How does your Ajax looks like?
I think you have to change

$update = $this->input->post('select_id');

into

$update = $this->input->post('select_update'); or $this->input->post('select_update[]');

because of the name attribute of your checkboxes and change

$this->db->where('id', $delete[$i]);

into

$this->db->where('id', $update[$i]);

or use it in a foreach

function update_multiple() {
    $updates = $this->input->post('select_update');

    foreach ($updates as $update) { 
        $data = array('notif' => 0);            
        $this->db->where('id', $update);
        $this->db->update('my_table', $data);               
    }
}   

Update #1

Your Ajax should look like this - keep scope on data of ajax

$.ajax({
  url : url,
  type: 'POST',
  data: $(".select_update:checked").serialize(),
  dataType: 'json',
  success: function(response) { ... },
  error: function (jqXHR, textStatus, errorThrown) { ... }
});
Sign up to request clarification or add additional context in comments.

7 Comments

I have upvoted your answer. Apparently both our original answers were partly correct and later we were inspired by each-other.
thanks @UfguFugullu for answer but That's not the problem after I replace still the same
@irwandwiyanto I think this answer is correct as well in view of your original question. This answer has not taken your edit about the AJAX request into account yet.
@UfguFugullu I've tried the script you corrected but it appears an ajax error
Can you show us your errors? Because I don't get any errors if the variable url is set before
|
0

It seems you have pasted your code from your delete_multiple method and this is why you use $delete instead of $update here

function update_multiple() {
    $update = $this->input->post('select_id');
    for ($i=0; $i < count($update) ; $i++) { 
        $data = array('notif' => 0);            
        $this->db->where('id', $delete[$i]);
        $this->db->update('my_table', $data);               
    }
}

Solution:

function update_multiple() {
    $update = $this->input->post('select_id');
    for ($i=0; $i < count($update) ; $i++) { 
        $data = array('notif' => 0);            
        $this->db->where('id', $update[$i]);
        $this->db->update('my_table', $data);               
    }
}

Inspired from UfguFugullu, who was apparently inspired by my answer as well:

select_update is needed instead of select_id

5 Comments

thanks @Lajos Arpad for answer but That's not the problem after I replace still the same
@irwandwiyanto your ajax contains id_pesan in the data. Apparently you will need that instead of select_id or my_table. Also, I would like to ask you to define what "still the same" means exactly in your context.
I want to update the data in the table based on the id in the checklist, but I doubt the problem on the data retrieval on ajax.
@irwandwiyanto I cannot see your problem. If you do not specify it exactly, then I cannot help you.
@irwandwiyanto I understand what you want to achieve, but your error message would be extremely helpful, for instance.

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.