ajax delete using codeigniter -
hi have delete thing, , im using on ajax delete data. problem wont delete on database, when refresh browser data remains, can me out figured thing?? here's controller below
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); session_start(); class news_and_events extends ci_controller { public function __construct(){ parent::__construct(); $this->load->library('form_validation'); $this->load->model('admin_model', 'am'); } public function index(){ if($this->session->userdata('logged_in')){ $this->data['title'] = 'news , events | spring rain global consultancy inc admin panel'; $this->data['logout'] = 'logout'; $session_data = $this->session->userdata('logged_in'); $this->data['id'] = $session_data['id']; $this->data['username'] = $session_data['username']; $this->data['alldata'] = $this->am->getalldata(); $this->load->view('pages/admin_header', $this->data); $this->load->view('content/news_and_events', $this->data); $this->load->view('pages/admin_footer'); }else{ redirect('login', 'refresh'); } } public function add(){ if($this->session->userdata('logged_in')){ $this->form_validation->set_rules('date', 'date', 'trim|required|xss_clean'); $this->form_validation->set_rules('event', 'event', 'trim|required|xss_clean'); $this->form_validation->set_rules('description', 'description', 'trim|required|xss_clean'); if($this->form_validation->run() == false){ $this->data['title'] = 'news , events | spring rain global consultancy inc admin panel'; $this->data['logout'] = 'logout'; $session_data = $this->session->userdata('logged_in'); $this->data['id'] = $session_data['id']; $this->data['username'] = $session_data['username']; $this->data['alldata'] = $this->am->getalldata(); $this->load->view('pages/admin_header', $this->data); $this->load->view('content/news_and_events', $this->data); $this->load->view('pages/admin_footer'); }else{ $array = array( 'date' => $this->input->post('date'), 'event' => $this->input->post('event'), 'description' => $this->input->post('description') ); $this->am->savedata($array); $this->session->set_flashdata('add_another',1); redirect('news_and_events', 'refresh'); } }else{ redirect('homepage', 'refresh'); } } public function delete(){ $id = $this->uri->segment(2); $this->am->delete($id); redirect(base_url().'news-and-events'); } }
and views
<script type="text/javascript"> $(document).ready(function(){ $("#add_another").click(function(){ }); }); function godelete(id){ var agree = confirm("are sure want delete this?"); if(agree){ $("#news-and-event"+id).fadeout('slow'); $.post('<?php echo base_url().'news-and-events/delete/'?>', {id:id}, function(){ }); }else{ return false; } } </script> <div class="container" > <br /> <br /> <br /> <ul id="nav"> <li><a href="<?php echo base_url().'homepage'?>" title="home"><h4>home</h4></a></li> <li><a href="<?php echo base_url().'news-and-events'?>" title="news , events"><h4>news , events</h4></a></li> <li><a href="" title="activities"><h4>activities</h4></a></li> </ul> <div class="starter-template"> <h1>news , events</h1> <?php if(!$this->session->flashdata('add_another')):?> <form action="<?php echo base_url().'news-and-events/add'?>" method="post"> <?php echo validation_errors('<div class="error">', '</div>');?> <table class="table-striped"> <tr> <td>date: </td> <td><input type="text" id="datepicker" name="date" value="<?php echo set_value('date');?>" /></td> </tr> <tr> <td> </td> </tr> <tr> <td >event: </td> <td ><input type="text" name="event" value="<?php echo set_value('event');?>" /></td> </tr> <tr> <td> </td> </tr> <tr> <td width="20%">description: </td> <td><textarea cols="30" rows="5" name="description" ><?php echo set_value('description');?></textarea></td> </tr> <tr> <td width="20%"> </td> <td><input type="submit" value="add" class="btn btn-success" /></td> </tr> </table> </form> <?php else: ?> <div id="add_another" style="float:left;"> <input type="button" value="add another" class="btn btn-primary" /> </div> <?php endif; ?> <br /> <br /> <table class="table" > <tr> <th>date</th> <th width="47%" >event</th> <th width="32%">description</th> <th>options</th> </tr> <?php foreach($alldata $x => $alldatas): ?> <tr id="news-and-event<?php echo $alldatas->id; ?>"> <td width="10%"><?php echo $alldatas->date; ?></td> <td style="text-align:left;"><?php echo $alldatas->event; ?></td> <td style="text-align:left;"><?php echo $alldatas->description; ?></td> <td width="10%"> <a href="<?php echo base_url().'news-and-events/edit/id/'.$alldatas->id;?>">edit</a> | <a href="javascript:;" onclick="return godelete('<?php echo $alldatas->id;?>');" >delete</a> </td> </tr> <?php endforeach; ?> </table> </div> </div><!-- /.container --> <script> var date = new date(); var currentmonth = date.getmonth(); var currentdate = date.getdate(); var currentyear = date.getfullyear(); $('#datepicker').datepicker({ mindate: new date(currentyear, currentmonth, currentdate), dateformat: "yy-mm-dd" }); </script>
and model delete database
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class admin_model extends ci_model{ public function savedata($array){ $this->db->insert('news_and_updates', $array); } public function getalldata(){ return $this->db->order_by("date", "desc") ->get('news_and_updates') ->result_object(); } public function delete($id){ $this->db->where('id', $id)->delete('news_and_updates'); } } ?>
im using $this->uri->segment(2); help? appreciated thanks
you sending data via post
trying delete based on uri segment
. try instead in controller:
public function delete(){ $id = $this->input->post('id'); $this->am->delete($id); redirect(base_url().'news-and-events'); }
Comments
Post a Comment