Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rockviju9's avatar

Triggering Session Flash Message ( using toastr.js )

When I keep refreshing the page it shows the same flash message >

public function create_deposit(){
    $data =$this->data;
    $customer_info = $data['customer'];
    $customer_id = $customer_info['customer_id'];
    $amount = get_data_value($_GET, 'amount');
    $amount = floatval($amount);
    if($amount<=0){
        $this->session->set_flashdata('error_message', "Invalid request (amount should be larger than zero)");
        redirect(base_url('portal/transaction/index'));
        return false;
    }
    $data['amount'] = $amount;
    if($this->check_submit()){
        $payment_data = array(
            's_id'=>get_data_value($_POST, 's_id'),
            'customer_id'=>$customer_id,
            'amount'=>$amount,
            'payment_mode'=>get_data_value($_POST, 'payment_mode'),
        );
        $dtime = DateTime::createFromFormat("d/m/Y", $_POST['document_date']);
        if(!empty($dtime)){
            $time_slot_stamp = $dtime->getTimestamp();
        }else{
            $time_slot_stamp = time();
        }
        $payment_data['add_timestamp'] = date("Y-m-d", $time_slot_stamp);

        $upload_filename = $this->upload_file();
        //print_r($upload_filename); die;
        if(!empty($upload_filename)){
            $payment_data['proof_image'] = $upload_filename;
        }
        $this->db->insert(DEPOSIT_TB, $payment_data);
        $this->session->set_flashdata('status_message', "Thank you. Your deposit was successful");
        redirect(base_url('portal/transaction/index').'?tab=deposits');
        
        
    }
    $data['page_title'] = "Deposit";
    $this->page->showpage($data);
}

Can I add something session so it shows after 1st submit button not every time I refresh any page in the backend?

And here is the controller-php file code -

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

class Customer_controller extends MY_Controller{ protected $data; protected $customer_id; protected $customer_name;

public function __construct(){
    parent::__construct();
    $this->load->model('customer_model');
    $this->load->model('access_log_model');
    $session_data = $this->session->get_userdata();
    $customerInfo = array();
    $customer_id = (isset($session_data['customer']['customer_id'])?$session_data['customer']['customer_id']:0);
    if(!empty($customer_id)){
        $customerInfo = $this->customer_model->get(array('customer_id'=>intval($customer_id)));
        if(empty($customerInfo)){
            $this->session->unset_userdata(array('customer_logged_in','customer'));
        }
    }
    $data['customer']=$customerInfo;
    $data['site_setting']=array();

    $data['status_message'] = $this->session->flashdata('status_message');
    $data['error_message'] = $this->session->flashdata('error_message');
    $this->data=$data;
}

public function index(){
    redirect('portal/login/index');
}

protected function check_login($return_url = '') {
    if(empty($return_url))
        $return_url = get_self_url();
    if (!$this->session->userdata('customer_logged_in')) {
        if($this->check_ajax())
            return $this->json_output_error('Please login');
        else
            redirect(base_url('portal/login?return_url='.urlencode($return_url)));
    }
    $customer_info = $this->session->userdata('customer');
    $this->customer_id=$customer_info['customer_id'];
    $this->customer_name = $customer_info['customer_name'];
}
public function login_with_customer($customer_info) {
    $session_data = array(
        'customer_logged_in' => true,
        'customer' => $customer_info
    );
    $this->session->set_userdata($session_data);
}
protected function add_log($log_data){
    $customer_info = $this->session->userdata('customer');
    if(isset($customer_info['customer_id'])){
        $log_data['add_timestamp'] = TIMESTAMP;
        $log_data['ip'] = get_ip();
        $log_data['customer_id'] = $customer_info['customer_id'];
        $log_data['customer_name'] = $customer_info['customer_name'];
        //$this->log_model->insert($log_data);
    }
}

}

0 likes
4 replies
MohamedTammam's avatar

To add a flash message use

session()->flash('status_message', 'Thank you. Your deposit was successful');

That will only uses it once.

rockviju9's avatar

@MohamedTammam not working, still showing.

I logged out first then tried Login again and it still showed after every reload/refresh page.

Tharshini_95's avatar

can try this way the session variable 'form_submitted' and set it to true once the form is submitted. then, check for the existence of this session variable before showiing the flash message. if the variable is not set, can show the flash message and set the 'form_submitted' session variable. if the variable is already set and don't show the flash message.

    // Check if the form is submitted for the first time
    if (!$this->session->has_userdata('form_submitted')) {
        $this->session->set_userdata('form_submitted', true);
        $this->session->set_flashdata('status_message', "Thank you. Your deposit was successful");
    }
1 like

Please or to participate in this conversation.