简体   繁体   中英

can't delete record in codeigniter using href

I am trying to delete particular record using href. But it's not working properly when i click on button its passing in URL like this

http://localhost/project_x/Organisation/Organisation/%3C?php%20echo%20base_url();?%3EOrganisation/delete_org?id=3

I don't know where is the mistake can anyone help me.It's passing id in URL is correctly.

Here is my controller:

function delete_org() {
   // Pass the $id to the org_delete() method
      $this->Org_model->org_delete($id);
      redirect('Organisation/organisation');
}

here is my redirect organisation function

public function organisation() {

    $data['organisations'] = $this->Org_model->getOrganisations();
    $this->load->view('template/header');
    $this->load->view("organisation", $data);
    $this->load->view('template/footer');
}

Here is my model:

function org_delete($id) {
    $this->db->where('id', $id);
    $this->db->delete('organisation');
}

And Here is my view buttons:

<?php
echo "<td class='text-center'><div class='btn-group btn-group-xs'><a href='#' data-logid='" . $org->id . "' data-target='#editGroups' data-toggle='modal' title='Edit' class='open_modal btn btn-default'><i class='fas fa-edit'></i></a>";
echo "<a href='<?php echo base_url();?>/Organisation/delete_org?id=$org->id'  class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";                                           
?>

Can anyone help me where i did mistake.

Thanks in advance.

Issue is you are adding php tag again in echo. change your delete anchor tag as below:

 echo "<a href='".base_url()."/Organisation/delete_org?id=$org->id'  class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";

Also in controller define $id

function delete_org() {
   // Pass the $id to the org_delete() method
      $id = $_GET['id'];
      $this->Org_model->org_delete($id);
      redirect('Organisation/organisation');
}

Hope this will help you :

Change view buttons like this:

<?php
echo "<td class='text-center'><div class='btn-group btn-group-xs'><a href='#' data-logid='" . $org->id . "' data-target='#editGroups' data-toggle='modal' title='Edit' class='open_modal btn btn-default'><i class='fas fa-edit'></i></a>";
echo "<a href='".base_url('Organisation/delete_org?id='.$org->id)."'  class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";

In controller :

function delete_org() { 
      $id = $this->input->get('id');
      $this->Org_model->org_delete($id);
      redirect('Organisation/organisation');
}

You may try this deleting record using codeigniter framework by data-attribute as bellow.

html

<a class="deletedata" data-id="<?php echo $data['id] ?>">delete</a>

js

$(".deletedata").on("click",function(){

  var id=$(this).data('id');
  var baseurl = $("#base_url").val();

  $.post(baseurl+"Products/dltProduct/"+id,function(id){
     window.location.reload();
  });
});

Here my Controller name is Products. you can define your name and than after define your method name.

Controller

public function dltProduct($id) {
  $this->db->where("id",$id);
  $this->db->delete("tbl_name");
}
function delete_org($id= NULL) {

   // Pass the $id to the org_delete() method
      $id = $this->input->post('id');
      $this->Org_model->org_delete($id);
      redirect('Organisation/organisation');
}
hope this helps you

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM