0
votes

I have a activate button in my CMS that allows the user to quickly toggle a active/inactive flag for news articles. When clicked the toggle makes a POST request to the url /news/toggle_active/$id, after which the news_model updates the boolean variable in the database for the specific articles id, and the controller redirects back to the index.

Controller

function toggle_active()
{
    $url = $this->uri->uri_string();
    $last_segment = count($this->uri->segment_array());
    $id = $this->uri->segment($last_segment);

    $row = $this->news_model->set_active($id);

    redirect('news/index', 'refresh');
}

Model

function set_active($id)
{
    $data = array(
           'display_flg' => 'NOT display_flg'
    );

    $this->db->where('id', $id);
    $this->db->update($this->tbl_news, $data);
}

The first time the activate button is clicked it works, but subsequent clicks do nothing. If I close the browser window and re-open the page, the button works again, but only for one click.

I haven't specifically enabled any caching, so I'm not sure whats going on.

If I remove the redirect from the end of the toggle_active() method, and output the current value of the display_flg variable, and POST to /news/toggle_active/$id it appears to do nothing on the first request. However on each subsequent refresh the variable changes values. Why is it not doing anything the first time the link is hit?

I found some people with what seems to be a similar issue over at the codeigniter forums, although there was no solid solution.

1
Have you tried this in a different browser? It's not something I've ever come across in CI, even with Caching enabled. - Gavin
Same problem in Chrome & Firefox, dunno what the problem is. If i paste in my code from the index() method (The code that builds my list of news articles) just before the redirect('news') it works fine. But I shouldn't have to rebuild my index within the toggle_active() method should I? It should do that when I redirect to it... - Jeemusu
If you can, comment out any security on your toggle_active method and paste the url including any segments into web-sniffer.net and click Submit. It will then tell you/us what type of redirect is happening and the response "before" it redirects. Hopefully that will help? - Gavin
Also... just fyi, toggle_active can be rewritten to pastebin.com/88xvuz9Y - I'm not sure if your using the segments for any particular reason, but assuming your url is http://www.domain.com/controller/toggle_active/1 then you can use function parameters instead. - Gavin
Thanks for the advice on function parameters, I had no idea! I couldn't find anything odd in the headers, and actually found a way around the problem this morning. I will submit an answer shortly. - Jeemusu

1 Answers

0
votes

I don't think this was a caching problem in the end. I made two major changes to fix the problem.

Firstly, I not check to see whether or not the database request was successful before redirecting.

I also re-wrote my the models set_active() method. Passing the SET clause as an array to the update functions second parameter wasn't working. I needed to use the set functions third parameter to stop the query from escaping the NOT part of the query.

I adjusted my functionality as follows:

Controller

function show_toggle($id)
{
    if($this->news_model->set_active($id)) 
    {
        redirect('news/index', 'refresh');
    }
}

Model

function set_active($id)
{
    $this->db->where('id', $id);
    $this->db->set('display_flg', 'NOT display_flg', FALSE);
    $query = $this->db->update($this->tbl_news);    

    return $query;
}