0

I am using CakePHP 2.3.6 & Bootstarp in a project. In the menu bar, there is a menu that I want when clicked, if the user is logged in, send an Ajax request and return a successful message, else load the login page in the modal plugin and let the user login. The login page can be on the same page, there is no need to use modal plugin to call a remote page.

Here is the pseudocode :

If(the user is logged in)
   send an Ajax request to a page and show return message
else
   show the login page in the modal window

I am using Bootstrap's modal plugin in another page :

<?php echo $this->Html->link($user['User']['name'],array('controller'=>'users','action'=>'view',$user['User']['id']),array('data-toggle'=>'modal','data-target'=>'.modal'));?>
.
.
.
<div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content"></div>
    </div>
</div>
.
.
.
<script>
   $('.modal').on('hide.bs.modal',function(){
      $(this).removeData('bs.modal');
   });
</script>

So, is there any way to do it ?

Please help me.

Thanks

1 Answers1

0

You can send an ajax request when clicking the menu button to an php page , in the php page you can test if the user is logged in , if is then :

<?php 
public function yourAjaxMethod () {
    if ( $this->UserModel->id ) {
        // do what you want to do end return the message to show
        $message = 'Your message to show when return success data';
    }else{

        $message = false;
    }
    return $message;
}
?>

And in your success ajax you test if the data is false or not

...,

success:function(data) {
if (data == false) {
     $('.modal').show();
}else{
     //append your message to any dom element
     $(selector).append(data);
}

}

your modal must be in the same page that the menu is, im not sur but try :)

Zakaria Wahabi
  • 213
  • 2
  • 12