Spyron10
Spyron10

Reputation: 150

Fancybox instance send POST to itself

I have a question. I'm using a Fancybox instance to show categories of tags. In this fancybox, users can edit the categories, delete them or create new ones.

The script that is executed in the fancybox is called with AJAX. That means that a PHP-file (called categories.php) is being executed while the browser doesn't redirect (it stays on tags.php). And here's where the problem kicks in:

I have a form on categories.php. If the user presses submit, then jQuery cancels the default event and send an AJAX request. This AJAX request should be send to this code (e.g. the code in categories.php).

How can I do that?

$(document).ready(function() {
    $('form').on('submit', function(event) {
        // cancel the default event (i.e. POST the form)
        event.preventDefault();
        var postData = $(this).serialize();
        var postUrl = this.href;

        var createCategory = $.ajax({
            url: postUrl,
            type: 'POST',
            data: postData
        });
        createCategory.done(function(result) {
            // code to reload the page
            // .replaceWith()?
            $('.testdiv').append(result);
            console.log('done');
        })
    });
});

Should the postUrl be $(this).href ? or Should I change that to point to the categories.php?

Upvotes: 2

Views: 121

Answers (1)

Ramon J. A. Smit
Ramon J. A. Smit

Reputation: 341

I think you should try to point at categories.php.

Upvotes: 1

Related Questions