简体   繁体   中英

Unable to display success message after data is inserted using codeigniter php

Hi i am having comment section in blog description page.If any user comment on the particular blog not able to display the success message it is redirecting directly to blog page.

Controller:

function addcomments()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('first_name','First Name' , 'required');
        $this->form_validation->set_rules('email','Email');
        $this->form_validation->set_rules('description','Description');
        if($this->form_validation->run()== FALSE)   
        {   
        $data['mainpage']='blogs';
        $this->load->view('templates/template',$data);
        }
        else
        {

            //insert the user registration details into database
            $data=array(
                'blog_id'=>$this->input->post('bl_id'),
                'first_name'=>$this->input->post('first_name'),
                'email'=>$this->input->post('email'),
                'description'=>$this->input->post('description'),
                );
            if ($this->blogs_model -> insertcomments($data))
            {
                if ($this->blogs_model->sendEmail($this->input->post('email')))
                {
                    //$this->flash->success('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
                    redirect("blog");
                }
                else
                {
                    //$this->flash->success('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                    redirect("blog");
                }

            }
            else
            {
                // error
                $this->flash->success('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                redirect('blog');
            }
        }
        }

To set a flash_session you must do it like this:

$this->session->set_flashdata('item', 'value');

And load the session library

$this->load->library('session');

https://www.codeigniter.com/user_guide/libraries/sessions.html

Try setting

$this->session->keep_flashdata('message');

in the constructor of the controller the user is being redirected to.

在重定向之前,请尝试使用$this->session->set_flashdata设置会话中的Flash消息,而不是$this->flash->success

So in the controller you can have in one function :

$flash=1;
redirect(base_url()."blog/".$flash);

And in the target function you can access the $flash value like this :

$flash= $this->uri->segment(3); 
if(!is_numeric($flash))
{
  redirect();       
}else{
   if($flash== 1){

   }
}

I put segment(3) because on your example $flash is after 2 dashes. But if you have for example this link structure : www.mydomain.com/subdomain/home/index/$flash you'll have to use segment(4) .

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM