简体   繁体   中英

how to call a javascript function from my controller?

I have a form in my view page on submit a function in a controller called function Login() is called This is my Login function

function Login()
{   
     $EmailId = $this->input->post('mailId');
     $Password = $this->input->post('password');
     $res=$this->friendsmodel->CheckLogin($EmailId,$Password);
     if($res==true)
     {

         $_SESSION['Authenticaton_user']="auth_user";
         $this->session->set_flashdata('item', 'Thanks for logging in');
                      //I want to call javascript function from here
         redirect('friends/Display_News');
     }
     else 
     {
      $this->session->set_flashdata('item', 'Username Or Password is invalid');
         redirect('friends');
     }

}

Now want to call a javascript function named topBar() from my if and else statement This is my script

 function topbar(message) 
{
       var alert = $('<div id="alert">'+message+'</div>');
        $(document.body).append(alert);
        var $alert = $('#alert');
        if ($alert.length) {
            var alerttimer = window.setTimeout(function() {
                $alert.trigger('click');
            }, 5000);
            $alert.animate({ height: $alert.css('line-height') || '50px' }, 200).click(function() {
                window.clearTimeout(alerttimer);
                $alert.animate({ height: '0' }, 200);
            });
        }
}

How to call a javascript from here

You can't call a javascript function from a PHP controller, however you can set some variable as a flag for your view to call the function:

function Login()
{   
     // I don't know how you interact with your views, so I will just
     // assume that it happens something like this:
     if($res==true)
     {
         // call the function
         $this->view->assign('callTopBar', true);
     }
     else 
     {
         // do not call the function
         $this->view->assign('callTopBar', false);
     }
}

And then, inside the view:

<?php if ($this->callTopBar): ?>
    <script type="text/javascript">
         topbar();
    </script>
<?php endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM