Waqleh
Waqleh

Reputation: 10151

Why do I have to click refresh button to get new data from database after submit

I am facing a problem with the submit form, I have a view that shows data from database with and edit button beside each record that pops up div with a form to update that specific record. The problem is that when I submit the form and the data is saved in the database, the view would show the the record with the old data, unless I hit the refresh button in the browser. (the codeigniter cache is set to FALSE)

the controller:

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Product extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('Table');
        $this->load->library('DX_Auth');

        // $this->load->library('pagination');
        $this->load->helper('form');
        $this->load->helper('url');

        // Protect entire controller so only admin, 
        // and users that have granted role in permissions table can access it.
        $this->dx_auth->check_uri_permissions();

        $this->load->model('Product_model');
        $this->load->library('Form_validation');
    }

    function index($sort = 0, $offset = 0) {
        $data['product'] = $this->Product_model->get_all()->result_array();

        if (isset($_POST['save'])) {
            $this->form_validation->set_rules('link', 'Link', 'trim|required|xss_clean|prep_url');
            $this->form_validation->set_rules('video', 'Video', 'trim|required|xss_clean|prep_url');
            if ($this->form_validation->run() === TRUE) {
                $post = $this->input->post();
                if(!isset($post['link_target_blank'])){
                    $post['link_target_blank'] = 0;
                }
                unset($post['save']);
                unset($post['id']);
                $this->Product_model->update_settings($this->input->post('id'), $post);
            }
        }

        // Load view
        $header_data['title'] = 'Game Product';
        $this->load->view($this->config->item('header_view'), $header_data);
        $this->load->view($this->config->item('blocks_view') . 'head_block');
        $this->load->view($this->config->item('menus_view') . 'main_menu');
        $this->load->view('backend/product', $data);
        $this->load->view($this->config->item('footer_view'));
    }

}

?>

Upvotes: 0

Views: 668

Answers (1)

diggersworld
diggersworld

Reputation: 13080

Because your get request:

$this->Product_model->get_all()->result_array(); 

Is before your insert/update request:

$this->Product_model->update_settings($this->input->post('id'), $post);

Therefore when you get all, the data isn't there yet.

If you put the get after your if statement - then it should work, e.g:

if (isset($_POST['save'])) 
{
    // all your POST processing and saving...
}

$data['product'] = $this->Product_model->get_all()->result_array();

Upvotes: 2

Related Questions