Long time I’ve been trying to delete an image from the database as well as the file path. There's no problem uploading the images. My controller:
public function deleteFile() {
if ($this->images_model->delete_file($user_id, $fileName)) {
$status = 'success';
$msg = 'File successfully deleted';
}else{
$status = 'error';
$msg = 'Something went wrong when deleting the file, please try again';
}
echo json_encode(array('status' => $status, 'msg' => $msg));
The Images_model is the following:
public function delete_file ($user_id, $fileName) {
$this->db->where('user_id', $user_id);
unlink("uploads/".$fileName);
$this->db->delete('images', array('user_id' => $user_id));
}
The problem might lie, I think, with my code for executing the delete. Here is the view where I’m trying to do something like the following. ‘main’ is pointing to the controller:
<input type="image" id="delete-profile-img" src="<?= base_url('/assets/images/delete-icon-gray.png') ?>" title="Delete Image" onclick="'$this->main->deleteFile'" >
Is it OK to use “onclick” while pointing to the function or do I need to follow that up with more complex java or ajax? Thanks in advance for any and all input.