1
votes

I want to redirect to view with script in codeigniter. Script first and then page redirection.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
           public function login()
           {
                  $this->load->view('login');
           }
}             

I tried this code but it's not working because page is redirected before script completes it's executions. So, How to show notification in alert.

                echo "<script>";
                echo "alert('User not Found');";
                echo "</script>";
                redirect('home/login');

I tried with this also,

 echo "<script>";
 echo "alert('User not Found');";
 echo "</script>";
 echo "<script>setTimeout(\"location.href = 'http://localhost/dealsnow/index.php/home/login';\",300);</script>";

Second way is working for me but i think it's not good way. So is there any other option available for redirection to view after script completes it's execution. Example : If i insert Form Data and then I want to show user that data is inserted properly in script and then i want the page to redirect. Script first and then page redirection.

2
Can run your script when view is call if your functionality is not change. - Mayank Vadiya
@MayankVadiya. I want to execute script first and then i want to redirect on another page. functionality is working properly only script disappear because page is redirected. - Bhavin

2 Answers

1
votes

Solve my question,

If we want to execute script first and then after some timeout. Ex. : if we want to show user that data is inserted properly in script and then i want the page to redirect.

                    echo "<script>";
                    echo "alert('Data Insereted Properly..!');";
                    echo "</script>";
                    $url = base_url().'/index.php/home/login';
                    header("refresh:3;url=$url");
0
votes

use flash data in your code.

// Set flash data 
$this->session->set_flashdata('error', 'User not Found');
// After that you need to used redirect function instead of load view such as 
redirect("home/login");

// Get Flash data on view 
$this->session->flashdata('error');

Hope it will help.