2

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?

Spyron10
  • 150
  • 9
  • `var postUrl = this.href;` should probably be `var postUrl = $(this).attr('action');` – skip405 Mar 04 '15 at 13:43
  • that works, just like pointing at the categories.php page. But now it gives an error about a Synchronous XMLHttpRequest on the mainthread being deprecated. I'll keep looking. thanks for the help. – Spyron10 Mar 04 '15 at 13:49

1 Answers1

1

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

Ramon J. A. Smit
  • 331
  • 3
  • 15
  • I did that, but still no dice. There's an error about Synchronous XMLHttpQuest on a main thread being depricated. I'll figure it out. Thanks for the help. – Spyron10 Mar 04 '15 at 13:47
  • Hmm, Thats weird. If i take a look like this on your code it looks good. Maybe @milkmannetje knows about this one? – Ramon J. A. Smit Mar 04 '15 at 13:51
  • Not sure if you're interested in this, but there are questions on the XMLHttpRequest already, so you could check it out here: http://stackoverflow.com/questions/24740773/synchronous-xmlhttprequest-on-the-main-thread-is-deprecated – Spyron10 Mar 04 '15 at 13:53
  • I will look into that one, indeed intresting! – Ramon J. A. Smit Mar 04 '15 at 13:55