Ajax 实例大全

1).更新记事版(主要是出现gif图提示一直正在更新数据)

<script language="javascript"> 
<!-- 
function updateNotebook()
{
$.ajax({ 
type:"POST", 
url:"<?php echo $this->createUrl('notebookUpdate')?>",
data:{
notebook: $('#notebook').val()
}, 
beforeSend:function(){
$("#notebookMessage").fadeIn(2000);
$("#notebookMessage").html('<span style="color:#FF0000"><img src="<?php echo $this->_baseUrl?>/static/admin/images/loading.gif" align="absmiddle">正在更新数据...</span>'); 
}, 
success:function(data){
$("#notebookMessage").html('<span style="color:#FF0000">'+data+'</span>');	
$("#notebookMessage").fadeOut(2000);	
} 
}); 
}
//--> 
</script>

  

public function actionNotebookUpdate ()
    {
        $notebook = $this->_gets->getPost('notebook');
        $model = Admin::model()->findByPk($this->_adminiUserId);
        $model->notebook = trim($notebook);
        if ($model->save()) {
            exit('更新完成');
        } else {
            exit('更新失败');
        }
    }

  

 

2)ajax单个以及批量删除信息:

views:

<td align="center"><input class="checkbox_site" value="<?php echo $one['id']; ?>" type="checkbox"   /></td>  //复选框删除
<td><input onclick="delet_vedio('<?php echo $one['id']; ?>');" class="delete" type="button"  /></td>    //单个删除按钮

 删除按钮:

<td colspan="2"><input style="margin-left:0px; margin-right:10px" type="button" onclick="delet_vedio()" class="but_w" id="shoulu" value="删除">

ajax函数:

 function delet_vedio(aid){ 
        var id = new Array();
        if(aid){
            id.push(aid);
        }else{
            $('.checkbox_site:checked').each(function(){
                id.push($(this).val());
            });
        }
        if(id.length < 1){
            return;
        }
        $.messager.confirm('提示', '确认要删除此视频吗?', function(r){  
            if (r){
                $.post(
                '<?php echo Yii::app()->createUrl("videoInter/Del") ?>',
                {'id':id},
                function(date){
                    if(date.status <= 0){
                        $.messager.alert('提示',date.delMessage);
                    } else {
                        //$.messager.alert('提示',date.delMessage);
                        setTimeout('ajax_load("page_right_inner","<?php echo $this->createUrl('videoInter/list'); ?>");', 1000);
                    }
                        
                },
                'json'
            );
            }
        });
    }

  

控制器动作:

public function actionDel() {
        $id = implode(",",$_POST['id']);
        Yii::import("application.interface.sobey.vms.*");
        $VMS = new SobeyVMS();
        $VMS->setDataType('json');
        $return = $VMS->deleteDataByIds($id);
        $criteria1 = new CDbCriteria();
        $criteria1->addInCondition('vms_id', $_POST['id']);//where vms_id in 1,2,3,4,5
        DnaMedia::model()->deleteAll($criteria1);
//或者: DnaMedia::model()->deleteByPk($_POST['ids'], $criteria);
die(json_encode($return)); }

  

 

 

3)ajax修改密码应用:

views:

<script>
    function updatePwd(){
        var oldpwd=$.trim($('input[name=oldpwd]').val());
        var newpwd=$.trim($('input[name=newpwd]').val());
        var renewpwd=$.trim($('input[name=renewpwd]').val());

        if(oldpwd.length == 0){
            $.messager.alert( '提示','请输入原密码!');               
        }else if(newpwd.length == 0){
            $.messager.alert('提示','请输入新密码!');
        }else if(renewpwd.length == 0){
            $.messager.alert('提示','请再输一次新密码!');
        }else if(newpwd != renewpwd){
            $.messager.alert('提示','两次新密码不一致!');
        }else{
            $.post(
            '<?php echo $this->createUrl('personal/resetPassword'); ?>',
{'Pwd[oldpwd]':oldpwd, 'Pwd[newpwd]':newpwd}, //控制器那边直接_POST['Pwd']接收 function(data){ $.messager.alert('提示',data.message); if(data.code == 1){ $('input[name=oldpwd]').val(''); $('input[name=newpwd]').val(''); $('input[name=renewpwd]').val(''); $('#changepwd-window').window('close'); } }, 'json' ); } } </script> <div style="padding-left:15px;" class="c_title mt10">邮箱账号:<input type="text" readonly="readonly" value="<?php echo Yii::app()->session['user']['email']; ?>" class="input_s" style="width:200px;" /></div> <div style="padding-left:15px;" class="c_title mt10">原始密码:<input type="password" value="" name="oldpwd" class="input_s" style="width:200px;" /></div> <div style="padding-left:15px;" class="c_title mt10">新 密  码: <input type="password" name="newpwd" value="" class="input_s" style="width:200px;" /></div> <div style="padding-left:15px;" class="c_title mt10">再次输入:<input type="password" value="" class="input_s" name="renewpwd" style="width:200px;" /></div> <div style="padding-left:15px;" class="c_title mt10"><input type="button" class="but_w" onclick="updatePwd();" value="确认"></div>

控制器:

public function beforeAction($action) {
        return $this->checkLogin();
    }
	

/**
     * 重置密码 
     */
    public function actionResetPassword() {
        $user = Yii::app()->session['user'];
        if (isset($_POST['Pwd'])) {              //主要是这个接收思想好
            
            $return = array('code' => -1, 'message' => '修改失败,请稍后再试!');
            $userInfo = ComUser::model()->find('uid=:uid', array(':uid' => $user['uid']));
            if (ComUser::model()->hashPassword($_POST['Pwd']['oldpwd'], $userInfo->salt) === $userInfo->password) {
                $userInfo->password = ComUser::model()->hashPassword($_POST['Pwd']['newpwd'], $userInfo->salt);
                if ($userInfo->save()) {
                    $return['code'] = 1;
                    $return['message'] = "修改成功!";
                } else {
                    $return['message'] = "网络忙,请稍后再试";
                }
            } else {
                $return['message'] = "请输入正确的原密码!";
            }
            die(json_encode($return));
        }
        $this->renderPartial('resetPassword');
    }

  

 3)))

评论的ajax提交

 
1.视图页面:

<form id="commentForm" name="cform" method="post" autocomplete="off">
<div class="cForm">
<div class="area">
<textarea name="comment" rows="3" class="pt" id="comment" ></textarea>
</div>

</div>
<div> 昵称:<input name="nickname" type="text" id="nickname" /> 邮箱:<input name="email" type="text" id="email" /></div>
<p class="ptn">
<input type="hidden" name="postId" id="postId" value="<?php echo $bagecmsShow['id']?>" />
<button class="button" type="button" id="postComment">发 布</button>
</p>
</form>

<script type="text/javascript">
$("#postComment").click(
function(){
$.post("<?php echo $this->createUrl('post/postComment')?>",
$("#commentForm").serializeArray(),function(res){
alert(res.message);
if(res.state == 'success')
window.location.reload();
},'json');	
}
);
</script>

 

 

 

2.PostController控制器:

public function action {

$nickname = trim( $this->_gets->getParam( 'nickname' ) );
$email = trim( $this->_gets->getParam( 'email' ) );
$postId = trim( $this->_gets->getParam( 'postId' ) );
$comment = trim( $this->_gets->getParam( 'comment' ) );
try {
if ( empty( $postId ) )
throw new Exception( '编号丢失' );
elseif ( empty( $nickname ) || empty( $email ) || empty( $comment ) )
throw new Exception( '昵称、邮箱、内容必须填写' );
$bagecmsPostCommentModel = new ;
$bagecmsPostCommentModel ->post_id = $postId;
$bagecmsPostCommentModel ->nickname = $nickname;
$bagecmsPostCommentModel ->content = $comment;
if ( $bagecmsPostCommentModel->save() ) {
$var['state'] = 'success';
$var['message'] = '提交成功';
}else {
throw new Exception( CHtml::errorSummary( $bagecmsPostCommentModel, null, null, array ( 'firstError' => '' ) ) );
}
} catch ( Exception $e ) {
$var['state'] = 'error';
$var['message'] = '出现错误:'.$e->getMessage();
}
exit( CJSON::encode( $var ) );
}

  

 
posted @ 2013-10-11 16:09  冯志强采桑子  阅读(510)  评论(0编辑  收藏  举报