0

I have modified the review form as popup modal in my custom module. And now How to submit a Review form in ajax and need to pass the submitted values to controller, Please provide me a solution. My js will look like below before ajax submission

js file

 define(['jquery', 'Magento_Ui/js/modal/modal'], function (jQuery, modal) {
    return function (config) {

        var customFunction = {
            ajaxSubmit: function () {
                var nickname = jQuery("#nickname_field").val();

                var review = jQuery("#review_field").val();

                var rating = jQuery(".field required review-field-ratings").val();

                alert(nickname);
                alert(review);
                alert(rating);
                var reviewurl = config.url;
                alert(reviewurl);
                jQuery.ajax({
                    url: reviewurl,
                    type: 'POST',
                    //Order id and reason passed to the controller
                    data: {'nickname': nickname, 'review': review, 'rating': rating, },
                    success: function (response) {
                        alert("successful");
                    },
                    error: function (response) {
                        alert("failed");
                    }
                });
            },

            options: {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                title: jQuery.mage.__('Product Review'),
                buttons: [{
                        text: jQuery.mage.__('submit'),
                        class: '',
                        click: function () {
                            //Ajax function call
                            customFunction.ajaxSubmit();
                            var close = this;
                            setTimeout(function () {
                                close.closeModal();

                            }, 3000);
                        }
                    }]
            }
        };
        jQuery(".product-reviews-summary").on('click', function () {
            modal(customFunction.options, jQuery('#popup-modal'));
            jQuery("#popup-modal").modal("openModal");

        });
        jQuery(".add-review").on('click', function () {
            modal(customFunction.options, jQuery('#popup-modal'));
            jQuery("#popup-modal").modal("openModal");

        });
        jQuery(".star-popup").on('click', function () {
            modal(customFunction.options, jQuery('#popup-modal'));
            jQuery("#popup-modal").modal("openModal");

        });
    };
});
0

use below code for ajax call and Post data to controller :

define(['jquery', 'Magento_Ui/js/modal/modal'], function (jQuery, modal) {
    return function (config) {

        var customFunction = {
            options: {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                title: jQuery.mage.__('Product Review'),
                 buttons: [{
                    text: $.mage.__('submit'),
                    class: '',
                    click: function(data) {

                        var form_data = jQuery("#form-id").serialize();


                        console.log(form_data);
                        jQuery.ajax({
                            url: "<?php  echo $block->getBaseUrl() . 'frontname/controllername/actionname';?>",
                            type: 'POST',
                            data: form_data,
                            success: function(data) {

                                console.log(data);
                            },
                            error: function(result) {
                                console.log('no response !');
                            }
                        });

                        this.closeModal();

                    }
                }]
            }
        };
        jQuery(".product-reviews-summary").on('click', function () {
            modal(customFunction.options, jQuery('#popup-modal'));
            jQuery("#popup-modal").modal("openModal");

        });
        jQuery(".add-review").on('click', function () {
            modal(customFunction.options, jQuery('#popup-modal'));
            jQuery("#popup-modal").modal("openModal");

        });
    };
});
  • Befor I have submitted the data directly to the core controller – Jaisa Dec 18 '17 at 9:36
  • Now, Am I need to override (Magento\Review\Controller\Product\Post.php) core controller in my custom module – Jaisa Dec 18 '17 at 9:37
  • I need to get the submitted form fields and need to pass it to the controller – Jaisa Dec 18 '17 at 9:42
  • you can directly post data to core controller – mansi Dec 18 '17 at 9:42
  • But in core controller they wrote code for form reload . But I dont want to reload the form after submitted so I need to override that, any suggestion – Jaisa Dec 18 '17 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.