Using ajax to execute a php code

Asked

Viewed 244 times

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.

  • change the function error for error: 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.

  • In $.ajax() ? Removing it caused the entire Javascript code not to run.

  • 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.

  • The alert window disappears very fast, so I couldn’t read it. I edited the post with more information.

  • Where does the value of $id?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.