mevr
mevr

Reputation: 1125

Delay and redirect

Is there anyway to display set flash data message first and then redirect to new page after a 5 secs delay in controller.

echo "Message";
sleep(5);
redirect();

Thanks

Upvotes: 1

Views: 9368

Answers (3)

Rashmy
Rashmy

Reputation: 125

I was stuck at the same place:

1.Type this for static url in view file:

<?php
 header("Refresh: 5; url=\"http://www.google.com\"");
 echo "You will be redirected to google in 5 seconds...";
?>

2.Type this for base_url in view file :

<?php
  header('Refresh:5; url= '. base_url().'/create_event'); 
  echo "You will be redirected in 5 seconds...";
?>

//DON'T Forget to put semi-colon at end.

Upvotes: 2

elddenmedio
elddenmedio

Reputation: 1030

When you are in your controller method to set the flashdata, send to time ins econds :

controller.php

public function do_something(){
    //do somthing
    $this->session->set_flashdata('message_id', 'Message');//message rendered
    $this->session->set_flashdata('seconds_redirect', 5);//time to be redirected (in seconds)
    $this->session->set_flashdata('url_redirect', base_url('controller/method'));//url to be redirected

    redirect('folder/temp_view', 'refresh');
}

in your view/folder/temp_view.php

<html>
<head>
    <meta http-equiv="refresh" content="<?= $this->session->flashdata('seconds_redurect');?>;url=<?= $this->session->set_flashdata('url_redirect')?>" />
</head>
<body>

    <?= $this->session->flashdata('message_id');?>

</body>
</html>

with this you pass the message variables, time in which you want to be redirected to another page and the url to where it is redirected

Upvotes: 3

shikhar
shikhar

Reputation: 2469

Since you are loading the page, you need to use javascript to redirect.

Put this code in your html view :

<script>setTimeout(function(){window.location.href='newpage.html'},5000);</script>

Upvotes: 2

Related Questions