CodeIgniter Forums
loading a view and redirecting - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: loading a view and redirecting (/showthread.php?tid=4790)



loading a view and redirecting - El Forum - 12-16-2007

[eluser]AtlantixMedia[/eluser]
Hi,

in my previous procedural projects when I needed to show a message to the user (no results found, no user by that name, etc) I would send a

Code:
echo "<meta http-equiv='refresh' content='3;URL=" . $URL . "'>";

echo whatever message, and the user would be redirected to xyz page after 3 seconds. how would I implement something like this with CI? how can I incorporate a refresh after x number of seconds after I loaded a view with the relevant message?

thanks in advance


loading a view and redirecting - El Forum - 12-16-2007

[eluser]Lovecannon[/eluser]
This would work: Add a <?=refresh?> in the head of your view, and if you need to redirect:
Code:
$data['refresh'] = "<meta http-equiv='refresh' content='3;URL=" . $URL . "'>";
$this->load->view('viewname', $data);
That should work


loading a view and redirecting - El Forum - 12-17-2007

[eluser]AtlantixMedia[/eluser]
great! thank you very much.


loading a view and redirecting - El Forum - 12-17-2007

[eluser]sandwormusmc[/eluser]
I've done this with JavaScript ... that way I can point a hidden form element to a specific CI function and check the post variables. It may not the best way to do it, but it works for me, and allows me to specify actions in my header files.

Here is some code:

Main header file:
Code:
<!---
function setAction(caller,action,identifier) {
    if(caller==="") {
        var submitMe = document.getElementById('hiddenForm');
    }
    else {
        var submitMe = document.getElementById(caller);
    }

    var changeAction   = document.getElementById(submitMe.elements['hiddenAction'].id);
    changeAction.value = action;
    submitMe.submit();
}
//--->

<body>
<!--- id=pageContainer closed in footer --->
<div id="pageContainer">
&lt;?
    $this->load->helper('form');
    // super secret action form to handle the buttons
    $attributes = array('id'=>'hiddenForm');
    echo form_open('',$attributes)."\n";
    echo '&lt;input type="hidden" name="action" value="" id="hiddenAction" /&gt;';
strlen($this->session->userdata('username'))?$whoami=$this->session->userdata('username'):$whoami=$this->input->post('username');    strlen($this->CI->session->userdata('last_activity'))?$lastvisit=$this->session->userdata('last_activity'):$lastvisit=$this->input->post('lastvisit');
    echo '&lt;input type="hidden" name="username" value="'.$whoami.'" id="hiddenName" /&gt;';
    echo '&lt;input type="hidden" name="lastvisit" value="'.$lastvisit.'" id="hiddenLastVisit" /&gt;';
    echo form_close();
    
    $welcomeMsg='Welcome, '.$whoami.'.  Your last visit was: '.date('r',$lastvisit);
?&gt;

Process login view, loaded when the user successfully authenticates:
Code:
&lt;?=$header?&gt;
&lt;?
        // show this when the user successfully logs in
        // print_r($this->CI->session->userdata);
        $username=$this->CI->input->post('username');
        echo "<div style='display:block;'>Thank you, $username, now logging you in.</div>";
        $vars=array(
            'username'=>$username,
            'action'=>'default',
            'prevAction'=>'processLogin'
        );
        $this->CI->session->set_userdata($vars);
?&gt;

&lt;!---
    window.setTimeout('login()',2000);

    function login() {
        setAction('','default','');
    }
//---&gt;