javascript - Redirect only after script Completes it's execution in codeigniter

59

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.

273

Answer

Solution:

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");
698

Answer

Solution:

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.

People are also looking for solutions to the problem: php - Fetch min() from other table with multiple relations

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.