Codeigniter after insert success message show in view

夙愿已清 提交于 2019-12-11 06:36:10

问题


I am new in Codeigniter and I need to show success and error message after insert data's in database.

How can I show the message in the view page?

This is my coding:

Model

function addnewproducts($data)
{
    if($data['product_name']!="" && $data['product_qty']!="" && $data['product_price']!="" && $data['date']!="")    
    {
        $res=$this->db->insert('product_list',$data);
        return $this->db->insert_id();
    }
    else
    {
        return false;
    }
}

Controller

function addnewproduct()
    {
        $this->load->model('products');
        $data['product_name'] = trim(strip_tags(addslashes($this->input->post('product_name'))));
        $data['product_qty'] = trim(strip_tags(addslashes($this->input->post('product_qty'))));
        $data['product_price'] = trim(strip_tags(addslashes($this->input->post('product_price'))));
        $data['datetime']=date('d-m-Y');
        $res = $this->products->addnewproducts($data);
        if($res==true)
        {
            $data['success'] = 'Successful';
            $this->load->view('addproduct',$data);
        }

    }

View

<p><?php echo $success; ?></p>

回答1:


There are many ways but below is which i recommend:

Set temp session in controller on success or error:

$res = $this->products->addnewproducts($data);
if($res==true)
{
    $this->session->set_flashdata('success', "SUCCESS_MESSAGE_HERE"); 
}else{
    $this->session->set_flashdata('error', "ERROR_MESSAGE_HERE");
}

In View you can display flashdata as below:

echo $this->session->flashdata('success');
or 
echo $this->session->flashdata('error');

Source : Codeigniter official website https://codeigniter.com/userguide3/libraries/sessions.html




回答2:


Controller:

function addnewproduct()
{
    $this->load->model('products');
    $data['product_name'] = trim(strip_tags(addslashes($this->input->post('product_name'))));
    $data['product_qty'] = trim(strip_tags(addslashes($this->input->post('product_qty'))));
    $data['product_price'] = trim(strip_tags(addslashes($this->input->post('product_price'))));
    $data['datetime']=date('d-m-Y');
    if($this->products->addnewproducts($data));
    {
        $this->session->set_flashdata('Successfully','Product is Successfully Inserted');
    }
    else
    {
       $this->session->set_flashdata('Successfully','Failed To 
        inserted Product');
    }
     // redirect page were u want to show this massage.
        redirect('Controller/Fucntion_name','refresh');
}// close function

view : On Redirect Page write This code top of Form

    <?php if($responce = $this->session->flashdata('Successfully')): ?>
      <div class="box-header">
        <div class="col-lg-6">
           <div class="alert alert-success"><?php echo $responce;?></div>
        </div>
      </div>
    <?php endif;?>



回答3:


I appreciate that you got your answer but I think flash data is bit old now, as we can use bootstrap to alert if any error and that looks good to on web page.

In controller

$res = $this->products->addnewproducts($data);
if($res==true)
{
  $this->session->set_flashdata('true', 'write_the_message_you_want');
}
else
{
  $this->session->set_flashdata('err', "write_the_message_you_want");
}

In View

<?php 
   if($this->session->flashdata('true')){
 ?>
   <div class="alert alert-success"> 
     <?php  echo $this->session->flashdata('true'); ?>
<?php    
} else if($this->session->flashdata('err')){
?>
 <div class = "alert alert-success">
   <?php echo $this->session->flashdata('err'); ?>
 </div>
<?php } ?>


来源:https://stackoverflow.com/questions/45254596/codeigniter-after-insert-success-message-show-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!