使用codeigniter php插入数据后无法显示成功消息

时间:2017-05-18 10:01:13

标签: php codeigniter

您好我在博客说明页面有评论部分。如果任何用户对特定博客的评论无法显示成功消息,则会直接重定向到博客页面。

控制器:

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');
            }
        }
        }

3 个答案:

答案 0 :(得分:2)

要设置flash_session,您必须这样做:

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

并加载会话库

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

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

尝试设置

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

在控制器的构造函数中,用户被重定向到。

答案 1 :(得分:1)

而不是public class MyFilter extends CommonsRequestLoggingFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) { //same mechanism as for request caching in superclass HttpServletResponse responseToUse = response; if (isIncludePayload() && !isAsyncDispatch(request) && !(response instanceof ContentCachingResponseWrapper)) { responseToUse = new ContentCachingResponseWrapper(response); } //outgoing request is logged in superclass super.doFilterInternal(request, responseToUse, filterChain); //log incoming response String rsp = getResponseMessage(responseToUse); LOGGER.info(rsp); } //equivalent to super.createMessage() for request logging private String getResponseMessage(HttpServletResponse rsp) { StringBuilder msg = new StringBuilder(); ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(request, ContentCachingResponseWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { int length = Math.min(buf.length, getMaxPayloadLength()); String payload; try { payload = new String(buf, 0, length, wrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException ex) { payload = "[unknown]"; } msg.append(";payload=").append(payload); } } } } 尝试在重定向之前使用$("#ButtonCreate").click(function() { $.post("next.php", <?php echo json_encode($issue); ?>, function(data, status) { document.getElementById("ResultBack").innerHTML = data; }); }); 在会话中设置Flash消息。

答案 2 :(得分:1)

所以在控制器中你可以拥有一个功能:

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

在目标函数中,您可以像这样访问$ flash值:

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

   }
}

我放了段(3),因为在你的例子中,$ flash是在2个破折号之后。但是,如果你有这样的链接结构: www.mydomain.com/subdomain/home/index/$flash ,你将不得不使用 segment(4)

希望有所帮助。