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');
}
});
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);
}
}
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) { ... }
});
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.