Passing php file in ajax

This is my code

<script>
$(document).ready(function() {



    // delete the entry once we have confirmed that it should be deleted

    $('.delete').click(function() {

        var parent = $(this).closest('tr');


        $.ajax({

            type: 'POST',

            url: 'delete.php', 

            data: 'ajax=1&delete=' + $(this).attr('id'),



            success: function() {

                parent.fadeOut(300,function() {

                    parent.remove();

                });

            }

        });        

    });


    $('.delete').confirm({



    });    

});

</script>

My question is why delete.php is not executing? It's in the same folder, do I need type absolure url to this file? But the best of it is that row in table is deleting, but php file is not executing. And second question is why this code is not working without this line:

  $('.delete').confirm({



        });   

In php file i got this to know is it executed:

<script>
alert('aaaa');
</script>

Or even I had change it on echo 'something'; but still not working

Answers


If the row is deleted the certainly the php file is executed and you have to check the url data as @Saifuddin has described.

use firebug console to see your result. and also you are not passing parameter to the success function

so change this

data: { ajax: 1, delete: $(this).attr('id')},

and also this

success: function(msg) {
   alert(msg)
   parent.fadeOut(300,function() {
      parent.remove();
   });
}

I assume that the php file is executed in your case, while better approach to define the url with absolute path like http://www.example.com/delete.php. however the way you pass data is not as per standard.

so replace this line.

data: 'ajax=1&delete=' + $(this).attr('id'),

with

data: { ajax: 1, delete: $(this).attr('id')},

and for debugging print $_POST variable in your php file.

hope it helps.


I would add an error handler to see what is happening with the ajax call, something like this:

error: function(jqXHR, textStatus, errorThrown) {
    console.log(errorThrown);
}

Keep in mind that the ajax call gets the current directory preppended to the relative uri you set. I assume you are sending the request to a working web server, i.e. using the HTTP protocol.

There is no confirm() jQuery function as far as I know. You usually do:

if (confirm('Are you sure you want to delete this item?')) do_delete();

The confirm() method returns true when the user selects OK, and false otherwise.

Cheers.


Need Your Help

Best way to populate a javascript typed array?

javascript literals typed-arrays

What is the best way to populate a javascript typed array with literal data?

Bash programmation (Cygwin): Illegal Character ^M

windows bash cygwin bc

I have a problem with a character. I think it's a conversion problem between dos and unix.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.