3

How to use model popup using ajax? Can any one help for this. Here below my code. How can i apply in this code model popup? And also post data get in popup. How it possible?

app/code/Vendor/Complany/view/frontend/web/js/example.js

define([
    'jquery',
    'Magento_Ui/js/modal/modal'
], function ($,example,modal) {
        return {
            exampleData:function(ajaxurl){
                $("#example").click(function(e){
                    e.preventDefault();
                    $.ajax({
                        url:ajaxurl,
                        type:'POST',
                        showLoader: true,
                        dataType:'json',          
                        success:function(){
                            alert('test');
                        }
                    });
                });
            }
        }
    }
);

app/cpde/Vendor/Company/view/frontend/templates/example/example.phtml

<div id="events_popup" style="display: none;" class="events_popup">

<script>
        require(
            ['jquery','Vendor_Company/js/example'],
            function($,example){
                var ajaxurl = '<?php echo //My URL using block function; ?>';
                example.exampleData(ajaxurl);
            }
        );
</script>
  • have you try with $("#events_popup") instead of $("#example")? – Rakesh Jesadiya Aug 23 '17 at 8:37
  • @RakeshJesadiya Here $("#example") is button id – Payal Patel Aug 23 '17 at 8:43
4
define([
    'jquery',
    'Magento_Ui/js/modal/modal'
], function ($,example,modal) {
        var options = {
            type: 'popup',
            responsive: true,
            innerScroll: true,
            buttons: [{
                text: $.mage.__('Continue'),
                class: 'primary action submit btn btn-default',
                click: function () {
                    this.closeModal();
                }
            }]
        };
        return {
            exampleData:function(ajaxurl){
                $("#example").click(function(e) {
                    e.preventDefault();
                    var $form = $('#form-validate');
                    var data = $('#form-validate').serialize();
                    if(!$form.valid()) return false;
                        $.ajax({
                            url:ajaxurl,
                            type:'POST',
                            showLoader: true,
                            dataType:'json',
                            data: data,
                            complete: function(data) {
                                $('#email_address').val("");
                                $("#events_popup").html('Thanks.').modal(options).modal('openModal');
                            },
                            error: function (xhr, status, errorThrown) {
                                console.log('Error happens. Try again.');
                            }
                        });
                });
            }
        }
    }
);

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.