0

Hi i have this delete thing, and im using it on ajax to delete the data. My problem is it wont delete on the database, when i refresh the browser the data remains, Can someone help me out figured this thing?? Here's my 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 and 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 and 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 my views

<script type="text/javascript">
  $(document).ready(function(){
    $("#add_another").click(function(){

    });
  });

  function goDelete(id){
    var agree = confirm("Are you sure you want to 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 and Events"><h4>News and Events</h4></a></li>
     <li><a href="" title="Activities"><h4>Activities</h4></a></li>
    </ul>
    <div class="starter-template">
      <h1>News And 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>&nbsp;</td>
            </tr>
            <tr>
              <td >Event: </td>
              <td ><input  type="text" name="event" value="<?php echo set_value('event');?>" /></td>
            </tr>
            <tr>
              <td>&nbsp;</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%">&nbsp;</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 as $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 my model to delete the 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 the $this->uri->segment(2); any help? is much greatly appreciated thanks

5
  • Knowing how to properly debug AJAX calls goes a long way. Please see this post: stackoverflow.com/a/21617685/2191572 Commented Mar 10, 2014 at 14:49
  • can you show ajax code? Commented Mar 10, 2014 at 14:50
  • here $.post('<?php echo base_url().'news-and-events/delete/'?>', {id:id}, function(){ }); Commented Mar 10, 2014 at 14:51
  • 1
    @kumar_v on line #12 of OP's view you can find $.post(). Bobcatnyxz, please truncate your question to only show relevant code and functions. Why do I need to see public function add() if you do not have an issue with it? Commented Mar 10, 2014 at 14:51
  • thanks anyways @MonkeyZeus i already solved it! Commented Mar 11, 2014 at 1:44

1 Answer 1

1

You are sending data via POST but you are trying to delete based on the uri segment. Try this instead in your controller:

  public function delete(){
    $id = $this->input->post('id');
    $this->am->delete($id);
    redirect(base_url().'news-and-events');
  }
Sign up to request clarification or add additional context in comments.

6 Comments

hey thanks men! it now really works! hmmm i thought i should use the $this->uri->segment();
Glad I helped. uri->segment would work if you were sending data via GET method and they were visible through the URL.
ahh i see just because im using ajax?? is that it?
ahh i see so ahm if the url is /news-and-events/delete/id/4 then that's the time the uri-segment will work??
Yes that's right! It does not have to do only with your AJAX call. It has to do that you are using POST method instead of GET at your AJAX call.
|

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.