1

I am learning jQuery by myself, and need a little help here, please.

I do not know why, but the "msg" variable in this code bellow is coming up empty in the end.

jQuery(function($) {
  $('#mailing').submit(function(e) {
    e.preventDefault();

    var msg = '';

    $.ajax({
      type: "POST",
      url: "ajx_mailing.asp",
      data: '[email protected]',
      cache: false,
      dataType: "text",
      complete: function(data){
        if (data.responseText = 'ok') {
          msg = 'The e-mail was included with success!';
        } else {
          msg = 'There is a problem, please check the e-mail!';
        }
      },
      error: function(data) {
        msg = 'Error! Please, try again latter!';
      },
    });

    /* http://www.ericmmartin.com/projects/simplemodal/ */

    $('#simplemodal').modal({
      minWidth: 410,
      minHeight: 120,
      position: ['30%',],
      overlayId: 'msgbox-overlay',
      containerId: 'msgbox-container',
      onShow: function(dialog) {
        var modal = this;
        $('.header', dialog.data[0]).append('Mailing List');
        $('.message', dialog.data[0]).append(msg);
        $('.ok', dialog.data[0]).click(function() {
          modal.close();
        });
      }
    });

  });
});

The modal message is blank? Do you know why?

Thanks!

1

1 Answer 1

1

$.ajax is an asynchronous function that it may complete at any time. So value of the variable msg will change after some time, before that ()$('#simplemodal').modal({ code will be executed that's why the value is missing. So just initialize the ()$('#simplemodal').modal({ after ajax success or error

jQuery(function($) {
    $('#mailing').submit(function(e) {
        e.preventDefault();
        var msg = '';
        $.ajax({
            type: "POST",
            url: "ajx_mailing.asp",
            data: '[email protected]',
            cache: false,
            dataType: "text",
            complete: function(data) {
                if (data.responseText = 'ok') {
                    msg = 'The e-mail was included with success!';
                } else {
                    msg = 'There is a problem, please check the e-mail!';
                }
                modal();
            },
            error: function(data) {
                msg = 'Error! Please, try again latter!';
                modal();
            },
        });
        /* http://www.ericmmartin.com/projects/simplemodal/ */

        function modal() {
            $('#simplemodal').modal({
                minWidth: 410,
                minHeight: 120,
                position: ['30%', ],
                overlayId: 'msgbox-overlay',
                containerId: 'msgbox-container',
                onShow: function(dialog) {
                    var modal = this;
                    $('.header', dialog.data[0]).append('Mailing List');
                    $('.message', dialog.data[0]).append(msg);
                    $('.ok', dialog.data[0]).click(function() {
                        modal.close();
                    });
                }
            });
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.