ThemeFuse / Unyson-Learning-Extension

This extension adds a Learning module to your theme. Using this extension you can add courses, lessons and tests for your users to take
2 stars 3 forks source link

Ajax for Quizes #8

Closed Priler closed 8 years ago

Priler commented 8 years ago

Hi!

Is there a way to make Quizes to be Ajax?

ghost commented 8 years ago

If the form handled by the FW_Form helper, you can try this solution

Priler commented 8 years ago

Seems like it handled with FW_Form, but this not works.

Priler commented 8 years ago

This error in console fw-form-helpers.js:41 Uncaught TypeError: Cannot read property 'errors' of undefined

Priler commented 8 years ago

For some reason ajax request always return 0 in Network tab. How this can be fixed?

ghost commented 8 years ago

The form is registered here on this action. Maybe that action is executed later than wp_ajax_* action

Priler commented 8 years ago

@moldcraft How it may be fixed?

ghost commented 8 years ago
jQuery(function($){
    var selector = 'form[data-fw-form-id="learning-quiz-quiz-form"]';

    $(document.body).on('submit', selector, function(e){
        e.preventDefault();

        var $form = jQuery(this);

        // get submit button
        {
            var $submitButton = $form.find(':submit:focus');

            if (!$submitButton.length) {
                // in case you use this solution http://stackoverflow.com/a/5721762
                $submitButton = $form.find('[clicked]:submit');
            }

            // make sure to remove the "clicked" attribute to prevent accidental settings reset
            $form.find('[clicked]:submit').removeAttr('clicked');
        }

        jQuery.ajax({
            type: "POST",
            url: $form.attr('action'),
            data: $form.serialize() + ($submitButton.length ? '&'+ $submitButton.attr('name') +'='+ $submitButton.attr('value') : ''),
            dataType: 'html'
        })
            .done(function(html){ window.hhh = html;
                $form.closest('.fw-learning-quiz').html(
                    $('.fw-learning-quiz', html).html()
                );
            }).fail(function(jqXHR, textStatus, errorThrown){
                alert(String(errorThrown));
            });
    });
});