Selva kumar
Selva kumar

Reputation: 385

I need to display an alert and redirect to a location from the controller

Alert is working fine but i can't able to redirect to location from controller.

class Test extends CI_Controller 
{   
         function __construct()
        {
           parent::__construct();
           $this->load->helper(array('form','url'));
        }    
        public function index()
        {
         print "<script type=\"text/javascript\">alert('Some text');</script>";
         redirect('reverse');
         die();
        }
}

Anyone help me to resolve this problem.

Thanks in advance.

Upvotes: 1

Views: 1574

Answers (2)

Nasser Mansouri
Nasser Mansouri

Reputation: 750

you can use flash variables to show message in next page that will be loaded , then if you want you can add some js script that redirect to new url after X seconds.

if you use a template engine, or multiple views for rendering template, in top of page before main content add some code for checking has_message and message_type and message flash data, and if they are set, show message with sent type (alert , info , success , error )

for flash variables in CI : www.codeigniter.com/user_guide/libraries/sessions.html?highlight=flash%20data#CI_Session::flashdata

Upvotes: 0

Saty
Saty

Reputation: 22532

You can use like below

public function index()
{
echo "<script>
    alert('Some text');
    window.location.href = 'reverse';// your redirect path here
</script>";
}

Upvotes: 1

Related Questions