Delete button not working in CodeIgniter

Here's my Delete button:

<td>
    <a href="javascript:void(0);" onclick="delete(<?php echo $data->emp_id;?>);">
        <button id="button_delete">Delete</button>
    </a>
</td>

Here's my Javascript code:

<script type="text/javascript">
    var url="<?php echo base_url();?>";
    function delete(id)
    {
        var r=confirm("Do you want to delete this employee?");
        if (r==true)
        {
            window.location = url+"index.php/admin_logins/employee4/"+id;
        }
        else
        {
            return false;
        }
    }
</script>

Here's my controller (admin_logins.php) method -

function employee4($emp_id)
{
    $this->employee_model->delete_employee($emp_id);
    $this->index();
}

And here's my model(employee_model.php) method -

public function delete_employee($emp_id)
{
    $this->db->where('employee.emp_id',$emp_id);
    return $this->db->delete('employee');
}

My table name is employee and my database is MySQL.

Column Name         Datatype
   id                 int
  emp_id             varchar(15)
  emp_name           varchar(50)
  emp_mobile_no      varchar(15)

Whenever I click the Delete button, nothing happens. What am I doing wrong?

Upvotes: 1

Views: 1694

Answers (4)

Sadman Hasan
Sadman Hasan

Reputation: 140

Faced same problem with delete button and Finally I figured out these codes and it's working exactly the way I want.

// ***** My Controller name : Main *****

public function delete_user()
{
    $this->model_users->deleteM($id);
 // whatever you like here.. redirect('') OR load view;
  }

// ***** Model *****

  public function deleteM($data)
  {
     $this->db->where('id', $this->uri->segment(3));
     $this->db->delete('employee');
  }

// ***** VIEW ******* 

<?php $bttn = '<button name="delete" onclick="ConfirmDelete()"></button>'; 
echo anchor("main/delete_user/$row->id", $bttn);?> 

If you need help on this VIEW you can ask anytime, for now I assume everything's cool !

// ***** OnClick JS Function ******

<script>
  function ConfirmDelete()
  {
    var x = confirm("Are you sure you want to delete?");
    if (x)
      return true;
    else
      return false;
  }
</script>

Upvotes: 1

Hardik Solanki
Hardik Solanki

Reputation: 3195

No need to pass $emp_id. Your controller function should be look like below :

function employee4()
{
    $emp_id = $this->uri->segment(3);

    $this->employee_model->delete_employee($emp_id);
    $this->index();
}

In your case uri segment value is 3

Upvotes: 3

George G
George G

Reputation: 7695

It's the function name delete I tested it and gave me this error:

Uncaught SyntaxError: Unexpected token delete

So try to name it with another name, ex: deleteEntry or deleteEmployee

delete is the javascript command so it's reserved word, and please always avoid to name functions or variables with reserved words

Alternatively I suggest to use the solution:

<td>
    <a href="index.php/admin_logins/employee4/<?php echo $data->emp_id;?>" 
       onclick="return confirm('Do you want to delete this employee?');">
        <button id="button_delete">Delete</button>
    </a>
</td>

Give a tags href, and onclick just return confirmation message

Upvotes: 1

Christian Burgos
Christian Burgos

Reputation: 1591

try this

$('#id_for_your_delete_button').on('click', function(){
     var id = $('#entry_id').val();
     $.ajax({
          type: 'POST',
          url: '<?= site_url("your_controller_here"); ?>',
          data: id
    });
});

Upvotes: 0

Related Questions