Welcome Guest, Not a member yet? Register   Sign In
Page not refreshing on Back button
#1

[eluser]mihaibaboi[/eluser]
Hello. I've got a problem with page refresh when I hit the Back button.

So, here's the example. I get a list of items from the database and put a delete button in front of every one. I hit delete, and I'm redirected to the same page, with all the records, except the one I've deleted. That's all fine, but if I hit Back I see the page, including the deleted record (of course, if I hit refresh it's back to normal).

I figured the page was cached and I tried to override that, but it did't work. Here's the code, so you can spot any possible mistakes.

The Controller:

Code:
<?php

class Main extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
        $this->output->set_header("Cache-Control: post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");        

        $query = $this->db->get('cds');

        foreach ($query->result() as $row)
        {
            $data["results"][$row->id]["id"] = $row->id;
            $data["results"][$row->id]["title"] = $row->titel;
        }
        
        $this->load->view('main_view', $data);
    }
    
    function delete()
    {
        $id = $this->uri->segment(3);
        
        $this->db->where('id', $id);
        $this->db->delete('cds');
        
        $this->index();
    }
}

The View:

Code:
<?php
header('Expires: Mon, 1 Jan 1990 00:00:00 GMT');
header("Cache-Control: no-cache, must-revalidate");
?>
<?php foreach($results as $result) : ?>
    <p>&lt;?php echo $result["title"]; ?&gt; <a href="main/delete/&lt;?php echo $result['id']; ?&gt;">Delete</a></p>
&lt;?php endforeach; ?&gt;

The code isn't the most elegant I've ever written, but it's just a mock-up for proof of concept (this is a problem that was indicated to me by a friend).

Also, you will notice that it's using CodeIgniter 2.0. For the record, it does exactly the same on 1.7.2.
#2

[eluser]rossmurphy[/eluser]
Try using base_url() in the link to the delete method instead of simply a relative uri

Or use the anchor method from the URL helper.
#3

[eluser]mihaibaboi[/eluser]
It doesn't have anything to do with the URL in the Delete button. The Delete method works just fine. Also, the index() method works fine. My problem is when I hit the back button. I thought that it just sends back to the previous URL (witch is http://localhost/codeigniter/index.php/main). In stead, it goes to that URL, but doesn't reload the controller, so I see the old page.

I hope I'm making myself clear. If you need any more details please ask.
#4

[eluser]rossmurphy[/eluser]
Change this:

$this->index();

to

redirect('main');
#5

[eluser]mihaibaboi[/eluser]
[quote author="rossmurphy" date="1291814542"]Change this:

$this->index();

to

redirect('main');[/quote]

Hey, this actually works. Don't know why yet. I'll have to go look at the docs. Thanks.
#6

[eluser]pickupman[/eluser]
That's because $this->index() just runs a function/method that happens to be another url and returns the html. Essentially using this syntax you could load every single method in a controller from one method, and have one crazy looking output. Using redirect() creates a 301 redirect which makes the browser go to a new url, and load the new page.




Theme © iAndrew 2016 - Forum software by © MyBB