0
Hello, I’m having a problem with how to use ajax to run a php code, I’m trying for hours to find the problem, but I can’t.
Code js
$.ajax({
url: 'deletarOrgs.php',
data: {
id: $id
},
type: 'POST',
success: function(){
alert("sucess");
},
error: function(){
alert("error");
}
});
Php code
<?php
include 'class/OrgaosColabDAO.php';
$DAO = new OrgaosColabDAO();
$DAO->deletarOC($_POST['id']);
?>
I tested php separately, it works right. When I run js, ajax executes the error function.
From now on, thank you.
Edit:
The purpose of ajax is to delete an object from my database, to control when I want to delete it, I click on a button that opens a modal (I am using the modal of bootstrap 3) where there are 2 buttons, yes or no. The ajax executes when I click the Yes button. The strange thing is when I don’t use the modal, running the ajax when I click the first button, the ajax works the right way.
$('#deletar-sim').click(function (){
$.ajax({
url: 'deletarOrgs.php',
data: {
id: $id
},
type: 'POST',
success: function(){
alert("a");
},
error: function (request, status, error){
alert(request.responseText);
}
});
});
I’ll give you my guess. Try removing the "$" from the Javascript part.
– Rodrigo Tognin
change the function
error
forerror: function (request, status, error){alert(request.responseText);}
perform once more and post the result to give one more clue of the possible cause of that there iron.– William Novak
In $.ajax() ? Removing it caused the entire Javascript code not to run.
– Nicolas Vilela
Try to put a return Boolean(true), in PHP, I believe that as nothing is returned Ajax does not identify that the operation was successful.
– Enderson Costa
The alert window disappears very fast, so I couldn’t read it. I edited the post with more information.
– Nicolas Vilela
Where does the value of
$id
?– bfavaretto