10

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>
asked Aug 23, 2017 at 7:39
2
  • have you try with $("#events_popup") instead of $("#example")? Commented Aug 23, 2017 at 8:37
  • @RakeshJesadiya Here $("#example") is button id Commented Aug 23, 2017 at 8:43

1 Answer 1

10
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.');
 }
 });
 });
 }
 }
 }
);
Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
answered Aug 23, 2017 at 10:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.