0

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

2
  • You can't do this. The browser is being sent a request then it is being processed and sent back, however you have a redirect function before your js would be sent back. Only way to achieve this is via an AJAX call to login, then use a JS redirect once it has alerted the user. Commented Jul 1, 2010 at 6:35
  • possible duplicate of How to call a JavaScript function from PHP? Commented Jun 24, 2012 at 1:26

1 Answer 1

3

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; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

$this->view->assign('callTopBar', true); here what does callTopBar denote is it the page that we view
No, it should be the name of the flag that you set. It could be anything, like shouldMyViewCallThisDamnFunctionOrNot.

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.