DCJones
DCJones

Reputation: 3451

triggering a Jquery function when condition is true

I am trying to trigger a Jquery fuction when the result of an "if" statement is true.

I have tried all sorts of ways with no success. I am using "Jbox" by Stephan Wagner which I have used many time without any issues so I think it must be the code I have written.

My code

var PassSet = '<?php echo $_SESSION['_amember_user']['passwordchange'];?>';
if(PassSet == 0) {
    $(document).ready(function() {
        var memberid = '<?php echo $_SESSION['_amember_user']['member_id'];?>';
        new jBox('Modal', {
            width: 350,
            height: 250,
            title: 'FlightX: Ack Alert',
            theme: 'TooltipBorder',
            closeButton: 'title',
            draggable: 'title',
            //trigger: 'click',
            animation: 'false',

            position: {
                x: 'center',
                y: 'center'
            },

            onCloseComplete: function() {
                this.destroy();
                $('#jBox-overlay').remove();
            },

            ajax: {
                url: 'flightx_change_password.php?UserID=' + memberid,
                reload: 'strict'
            }
        });
    }); 
}

Many thanks in advance for your time.

Upvotes: 0

Views: 185

Answers (1)

Louys Patrice Bessette
Louys Patrice Bessette

Reputation: 33933

The $(document).ready() handler is misplaced.

That is an handler "waiting" for the event that occurs when the document is fully loaded (except images) so that the DOM elements are all present and the script can run.

So the idea is to place the value (the var PassSet) and the condition within that ready handler.

Now, the value passed by the PHP echo should not be wrapped with quotes. That makes it a string... Better pass it as an integer.

EDIT
If you want to use Ajax to load the content of the modal and open it, here is how I would try to do it:

$(document).ready(function() {

  var PassSet = <?php echo $_SESSION['_amember_user']['passwordchange'];?>;  // Integer here! No quotes.

  // Check the value in console:
  console.log(PassSet);
  console.log(typeof(PassSet));  // Should give "number" in console.

  if(PassSet == 0) {

    var memberid = '<?php echo $_SESSION['_amember_user']['member_id'];?>';

    $.ajax({
      url: 'flightx_change_password.php',
      method: 'post',
      data:{UserID: memberid},
      success: function(response){

        new jBox('Modal', {
          width: 350,
          height: 250,
          title: 'FlightX: Ack Alert',
          theme: 'TooltipBorder',
          closeButton: 'title',
          draggable: 'title',
          //trigger: 'click',
          animation: 'false',

          content: response // Response from the Ajax request

          position: {
              x: 'center',
              y: 'center'
          },

          onCloseComplete: function() {
              this.destroy();
              $('#jBox-overlay').remove();
          }
        })
      }

    });

  }
});

Upvotes: 1

Related Questions