简体   繁体   English

使用Codeigniter PHP插入数据后无法显示成功消息

[英]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: 要设置flash_session,您必须这样做:

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

And load the session library 并加载会话库

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

https://www.codeigniter.com/user_guide/libraries/sessions.html 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值:

$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. 我将segment(3)放进去,因为在您的示例中$ flash在2个破折号之后。 But if you have for example this link structure : www.mydomain.com/subdomain/home/index/$flash you'll have to use segment(4) . 但是,例如,如果您具有以下链接结构: www.mydomain.com/subdomain/home/index/$flash ,则必须使用segment(4)

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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