Stef Van Gils
Stef Van Gils

Reputation: 41

Ajax function not called

Hey guys i got a problem with an ajax function call. I try to reach the page "delete.php", but it never gets called. Here is my script code:

 <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <script>
            function deleteSpark(sparkID) {

                    var answer = confirm(sparkID);
                    //Send the user to the correct page when he presses 'yes / oke'
                    if(answer==0){
                        var ajax= $.ajax({ type: "post",
                            data: {sparkid: sparkID },
                            url: 'delete.php'
                        });
                        ajax.done(function(){
                            window.location.href = "/sparks.html";
                        });

                        ajax.fail(function(){
                            window.location.href = "/index.html";
                        })
                    }
                    /* OR*/



                    /* this method after all other code in handler*/
                // window.location.href = "/delete.php?sparkid=" + sparkID;
                // window.location.href = "/sparks.html";
                //Returns false so no events will be executed.
                return false;

            }

I call it then with a button: delete 8

And in my php file "delete.php" i do this: $sparkID = $_POST['sparkid'];

but it never gets called, because I try to echo and do some other things that won't work. Can somebody help me?

Upvotes: 0

Views: 74

Answers (3)

Sebastien B.
Sebastien B.

Reputation: 476

I assume, if the return false isn't for prevent the event, that you want the return false when the confirm isn't "yes". So I moved the return false in a else statement.

Now as pointed by @Sergey Yarotskiy your if statetement is the real problem because you are waiting for a 0; you can combine ( if you need the variable answer somewhere else ) like so : if(answer = confirm(sparkID)){ or if it's not needed like so : if(confirm(sparkID)){.

Hope this will help you.

This should fix it :

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <script>
            function deleteSpark(sparkID) {
                    //Send the user to the correct page when he presses 'yes / oke'
                    if(answer = confirm(sparkID)){
                        var ajax= $.ajax({ type: "post",
                            data: {sparkid: sparkID },
                            url: 'delete.php'
                        });
                        ajax.done(function(){
                            window.location.href = "/sparks.html";
                        });

                        ajax.fail(function(){
                            window.location.href = "/index.html";
                        })
                    }else{
                     return false;
                    }

            }

Upvotes: 1

Sergey Yarotskiy
Sergey Yarotskiy

Reputation: 507

'confirm' will return true or false instead of 0, so change condition to

if (answer) {
    // do staff
}

Upvotes: 0

versalle88
versalle88

Reputation: 1137

I don't have enough reputation to comment yet. Can you inspect element (Firefox/Chrome), then look at the Network tab while the AJAX is running? This should show the data being sent, which page it is being sent to, etc... and should help you debug.

You might want to also try using an absolute URL initially to make sure it's getting to the right page.

Thanks,

Andrew

Upvotes: 1

Related Questions