yii 中设置提示成功信息,错误提示信息,警告信息

方法一:

<?php
Yii::app()->user->setFlash(‘success’,”Data saved!”); 设置键值名为success的临时信息.在getFlash后删除. 可以定义多种不同的键值名对象的消息.
?> <?php if(Yii::app()->user->hasFlash(‘success’)):?> 消息存在里,
<div>
<?php echo Yii::app()->user->getFlash(‘success’); ?> 输出消息内容,输出后,就不存在了.
</div>
<?php endif; ?> <?php //这是一段,在显示后定里消失的JQ代码,已集成至Yii中.
Yii::app()->clientScript->registerScript(
‘myHideEffect’,
‘$(“.info”).animate({opacity: 1.0}, 3000).fadeOut(“slow”);’,
CClientScript::POS_READY
);
?>

方法二:

http://www.yiiframework.com/forum/index.php/topic/22105-%E8%AF%B7%E9%97%AEyii%E9%87%8C%E9%9D%A2%E6%9C%89%E6%B2%A1%E6%9C%89%E6%98%BE%E7%A4%BA%E4%B8%B4%E6%97%B6%E6%B6%88%E6%81%AF%EF%BC%8C%E5%87%A0%E7%A7%92%E5%90%8E%E8%B7%B3%E8%BD%AC%E7%9A%84%E5%8A%9F%E8%83%BD%E5%95%8A%EF%BC%9F/

1,在CWebUser扩展类中

        publicfunction pushMessage($string=null, $type='successMsg')
{
if($string!=null){
if($this->hasFlash($type))
$old = $this->getFlash($type);
else
$old = array();
array_push($old, $string);
$this->setFlash($type, $old);
}
}
publicfunction showPutMsg()
{
$show ='';
$successName='successMsg';
$noticeName='noticeMsg';
$errorName='errorMsg'; if($this->hasFlash($successName)){
$show.='<div class="flash-success">';
$msg=(array) $this->getFlash($successName);
$show.='<ul>';
for($i=0; $i<count($msg); $i++){
$show.='<li>'. $msg[$i].'</li>';
}
$show.='</ul></div>';
}
if($this->hasFlash($noticeName)){
$show.='<div class="flash-notice">';
$msg=(array) $this->getFlash($noticeName);
$show.='<ul>';
for($i=0; $i<count($msg); $i++){
$show.='<li>'. $msg[$i].'</li>';
}
$show.='</div>';
}
if($this->hasFlash($errorName)){
$show.='<div class="flash-error">';
$msg=(array) $this->getFlash($errorName);
$show.='<ul>';
for($i=0; $i<count($msg); $i++){
$show.='<li>'. $msg[$i].'</li>';
}
$show.='</div>';
}
return $show;
} publicfunction putSuccessMsg($string=null)
{
$this->pushMessage($string,'successMsg');
}
publicfunction putNoticeMsg($string=null)
{
$this->pushMessage($string,'noticeMsg');
}
publicfunction putErrorMsg($string=null)
{
$this->pushMessage($string,'errorMsg');
}

2,在controller中,可以有三种提示信息

Yii::app()->user->putSuccessMsg('操作成功啦!');
Yii::app()->user->putNoticeMsg('操作警告...');
Yii::app()->user->putErrorMsg('操作失败了!');

3,最后在view中统一弹出便可。

<?php echo Yii::app()->admin->showPutMsg();?>

一般不要采用js弹窗形式,用户体验不是很好。

方法三:

public function redirect_message($message='成功', $status='success',$time=3, $url=false )
{ $back_color ='#ff0000'; if($status =='success')
{
$back_color= 'blue';
} if(is_array($url))
{
$route=isset($url[0]) ? $url[0] : '';
$url=$this->createUrl($route,array_splice($url,1));
}
if ($url)
{
$url = "window.location.href='{$url}'";
}
else
{
$url = "history.back();";
}
echo <<<HTML
<div>
<div style="background:#C9F1FF; margin:0 auto; height:100px; width:600px; text-align:center;">
<div style="margin-top:50px;">
<h5 style="color:{$back_color};font-size:14px; padding-top:20px;" >{$message}</h5>
页面正在跳转请等待<span id="sec" style="color:blue;">{$time}</span>秒
</div>
</div>
</div>
<script type="text/javascript">
function run(){
var s = document.getElementById("sec");
if(s.innerHTML == 0){
{$url}
return false;
}
s.innerHTML = s.innerHTML * 1 - 1;
}
window.setInterval("run();", 1000);
</script>
HTML;
}

把代码拷入 CController.php 文件下

方法四:
http://www.yiiframework.com/forum/index.php?/topic/8949-%E8%80%81%E5%A4%A7%EF%BC%8Cyii%E6%9C%89%E6%B2%A1%E6%9C%89%E8%B1%A1qeephp%E4%B8%AD%E7%9A%84-redirectmessage%E4%BF%A1%E6%81%AF%E6%8F%90%E7%A4%BA%E5%87%BD%E6%95%B0/page__p__44880
publicfunction redirectMessage($message, $url, $delay=5, $script='')
{
$this->layout=false;
if(is_array($url))
{
$route=isset($url[0])? $url[0]:'';
$url=$this->createUrl($route,array_splice($url,1));
}
return $this->render('/redirect', array(
'message'=> $message,
'url'=> $url,
'delay'=> $delay,
'script'=> $script,
));
}
把这个方法放到CBaseController中(也就是你的父控制器中,这样,所有的控制器都以调用了, 就相当于全局函数了)

From: http://www.cnblogs.com/Alight/archive/2013/01/18/2866421.html

上一篇:Hadoop案例(四)倒排索引(多job串联)与全局计数器


下一篇:Scanf函数输入字符串