I am using a MVC and I have a button that is using an AJAX call to remove an uploaded image on the site.
This is my Model:
public function remove_document($documentID, $documentName)
{
$objData = $this->objDB
-> setStoredProc('attritionRemoveDocument')
-> setParam('documentID', $documentID)
-> setParam('documentName', $documentName)
-> execStoredProc()
-> parseXML();
return $objData->data->response;
}
The response back from this is either true
or false
.
Here is my controller:
public function deleteFile()
{
// Get the documentID we are removing
$documentID = $this->input->post('documentID');
$documentName = $this->input->post('documentName');
// Check if the file is even there
if (file_exists('./uploads/'.$documentName)){
// Remove file
unlink('./uploads/'.$documentName);
$removeFile = $this->submit_model->remove_document($documentID, $documentName);
return $removeFile;
}
}
And finally, my AJAX Call:
$('[name=deleteDocument]').click(function() {
var documentID = $(this).attr('documentID'),
documentName = $(this).attr('documentName');
//Delete the image
$.ajax({
type: 'POST',
url: '../deleteFile',
dataType: 'xml',
data: {
'documentID': documentID,
'documentName': documentName
},
success: function(msg) {
// On Success, remove the current file section
console.log(msg);
}
});
});
When i echo
the $removeFile
value in the controller, I see the true/false
value however it never makes it to the success function of the AJAX call.
Any ideas?
in Controller
if the result is true
echo "OK"
if the result is false
echo "NO"
in your View and in success part
success: function(msg) {
if(msg=="OK"){
alert("DELETED");
}else{
alert("NOT deleted");
}
Your controller should not return BOOL if you are using it with ajax , Your AJAX call , should control the result by success method .