javascript - delete function not working in codeigniter -
my delete function not working. after clicking confirm message it's not deleting.
my view looks this:
<div id="selected-color" > <?php if ($this->session->userdata('edit_colors')) { foreach ($this->session->userdata('edit_colors') $color) { ?> <div class="col-md-2 selected-color"> <div class="color-box" style="background-color:<?php echo $color; ?>"></div> <div class="selected-color-btn" color='<?php echo $color; ?>' data-toggle="modal" > <input type="hidden" name="color_id" value="<?php echo $color; ?>"> <a class="fa fa-close" data-toggle="modal" onclick='return deleteitem()' name="delete" id="title" img="img1446224811.jpg" big-img="" title="delete" href="<?php echo base_url();?>admin_control/delete_color/<?php echo $result->id;?>/<?php echo $color;?>"><i class="fa fa-trash-o" data-toggle="tooltip" data-placement="top" title="delete"></i></a></span> </div> </div> <?php } }?> </div> </div>
my javascript looks this:
<script type="text/javascript"> function deleteitem() { var checkstr = confirm('are sure want delete this?'); if (!checkstr) { return false; } } </script>
my controller looks this:
public function delete_color($id = null) { $this->roxmodel->delete_color($id); redirect('admin_control/view_images'); }
my model looks this:
public function delete_color($id) { $this->db->where('product_color.id', $id); if ($this->db->delete('product_color')) { return true; } else { return false; } }
my table looks this:
id | product_id | color | ---------------------------- 711 | 341 | #f79646 | 712 | 341 | #31859b | 713 | 341 | #c0504d | 745 | 343 | #c0504d | 749 | 347 | #1f497d | 779 | 348 | #c0504d |
i guess problem in anchor tag ...
my controler edit function looks this...
public function edit_gallery($id) { $data['active_mn']='add_images'; $data['active_mn']='view_images'; if($_post) { $this->form_validation->set_rules('image','image','callback_is_upload_image'); if($this->form_validation->run()==true) { if($this->roxmodel->image_update1()) { $this->session->unset_userdata('image'); //$this->session->unset_userdata('colors'); $this->session->set_flashdata('message','updated successfully'); redirect('admin_control/view_images'); } else { $this->session->set_flashdata('message','update failed'); redirect('admin_control/view_images'); } } } if($id){ $id=$this->uri->segment(3); $data['result']=$this->roxmodel->view_gallery_id($id); //$data['query']=$this->admin_model->get_all('products')->result(); // $data['detail']=$this->roxmodel->get_products(null,null,$id); $colors=$this->roxmodel->get_product_color(null,$id)->result(); if($colors) { foreach ($colors $row){ $color[]=$row->color; } $this->session->set_userdata('edit_colors',$color); } $this->session->set_userdata('image',json_decode($data['result']->image)); $data['category']=$this->roxmodel->get_super_category(); $data['sub_category']=$this->roxmodel->get_sub_category(); } $this->load->view('edit_image',$data); }
see view there how can corresponding id value
see how delete records. have made function in base model this:
public function delete($id){ $filter = $this->_primary_filter; $id = $filter($id); if (!$id) { return false; } $this->db->where($this->_primary_key, $id); $this->db->limit(1); $this->db->delete($this->_table_name); }
then in controller initialize :
public function delete($id) { $this->model->delete($id); redirect('admin/dashboard'); }
in view anchor tag :
<td><?php echo btn_delete('controller/delete/' . $user->id); ?></td>
also have created helper edit , delete button:
function btn_delete ($uri) { return anchor($uri, '<i class="glyphicon glyphicon-remove"></i>', array( 'onclick' => "return confirm('you delete record. cannot undone. sure?');" )); }
my_model.php
<?php class my_model extends ci_model { protected $_table_name = ''; protected $_primary_key = 'id'; protected $_primary_filter = 'intval'; protected $_order_by = ''; public $rules = array(); protected $_timestamps = false; function __construct() { parent::__construct(); } public function array_from_post($fields){ $data = array(); foreach ($fields $field) { $data[$field] = $this->input->post($field); } return $data; } public function array_from_get($fields){ $data = array(); foreach ($fields $field) { $data[$field] = $this->input->get($field); } return $data; } public function get($id = null, $single = false){ if ($id != null) { $filter = $this->_primary_filter; $id = $filter($id); $this->db->where($this->_primary_key, $id); $method = 'row'; } elseif($single == true) { $method = 'row'; } else { $method = 'result'; } return $this->db->get($this->_table_name)->$method(); } public function get_by($where, $single = false){ $this->db->where($where); return $this->get(null, $single); } public function save($data, $id = null){ // set timestamps if ($this->_timestamps == true) { $now = date('y-m-d h:i:s'); $id || $data['created'] = $now; $data['modified'] = $now; } // insert if ($id === null) { !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = null; $this->db->set($data); $this->db->insert($this->_table_name); $id = $this->db->insert_id(); } // update else { $filter = $this->_primary_filter; $id = $filter($id); $this->db->set($data); $this->db->where($this->_primary_key, $id); $this->db->update($this->_table_name); } return $id; } public function delete($id){ $filter = $this->_primary_filter; $id = $filter($id); if (!$id) { return false; } $this->db->where($this->_primary_key, $id); $this->db->limit(1); $this->db->delete($this->_table_name); }
}
Comments
Post a Comment