在视图中显示插入成功消息后的Codeigniter
我是Codeigniter的新手,我需要在数据库中插入在视图中显示插入成功消息后的Codeigniter,codeigniter,insert,message,Codeigniter,Insert,Message,我是Codeigniter的新手,我需要在数据库中插入数据后显示成功和错误消息 如何在视图页面中显示消息 这是我的代码: 型号 function addnewproducts($data) { if($data['product_name']!="" && $data['product_qty']!="" && $data['product_price']!="" && $data['date']!="") {
数据后显示成功和错误消息
如何在视图
页面中显示消息
这是我的代码:
型号
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;
}
}
控制器
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);
}
}
查看
<p><?php echo $success; ?></p>
有很多方法,但我推荐以下方法:
成功或出错时在控制器中设置临时会话:
$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");
}
在视图中,您可以按如下方式显示flashdata:
echo $this->session->flashdata('success');
or
echo $this->session->flashdata('error');
资料来源:Codeigniter官方网站有很多方法,但我建议如下:
成功或出错时在控制器中设置临时会话:
$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");
}
在视图中,您可以按如下方式显示flashdata:
echo $this->session->flashdata('success');
or
echo $this->session->flashdata('error');
资料来源:Codeigniter官方网站我很感激您得到了答案,但我认为flash数据现在有点旧了,因为我们可以使用bootstrap在出现任何错误时发出警报,这在网页上看起来很好
内部控制器
鉴于
我很感激您得到了答案,但我认为flash数据现在有点旧了,因为我们可以使用引导程序在出现任何错误时发出警报,这在网页上看起来很好
内部控制器
鉴于
控制器:
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
视图:
在重定向页面上,在表单顶部编写此代码
<?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;?>
控制器:
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
视图:
在重定向页面上,在表单顶部编写此代码
<?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;?>
在模型函数addnewproduct($data=array())中,缺少$data@wolfgang1983:你能告诉我怎么加上这个吗?感谢您回复模型函数addnewproduct($data=array())中缺少的$data@wolfgang1983:你能告诉我怎么加上这个吗?谢谢你的回复谢谢你的回复你没有得到预期的结果吗@Ramesh SYes我得到了SudeepSagar@SudeepSagar先生,我尝试了你的视图代码,它给了我解析错误:语法错误,意外的'else'(T_else)我已经编辑了我的答案,需要为if语句添加结束标记,请尝试再次运行。如果在查看页面代码中,请将“}”放在其他位置之前谢谢您的回复您没有得到预期的结果吗@Ramesh SYes我得到了SudeepSagar@SudeepSagar先生,我尝试了你的视图代码,它给了我解析错误:语法错误,意外的'else'(T_else)我已经编辑了我的答案,需要为if语句添加结束标记,请尝试再次运行。如果在查看页面代码时,只需将“}”放在其他项之前