Ajax以yii更新内容

时间:2015-09-08 08:27:10

标签: php ajax yii

我遇到了ajax的问题​​,它不会更新保存的内容。数据在数据库中保存得很好。它仅在我刷新页面时显示。 回应评论的代码

<?php
echo '<div><h3><b><u>Comments</u></b></h3></div>';
$commentList = Comments::model()->findAllByAttributes(array('offereventid'=>$id));
foreach($commentList as $Listdata2)
{   
    $usercomment = $Listdata2['comment']; 
    $usercommentid = $Listdata2['id']; 
    $usercomtname = $Listdata2['name'];
    $usercommentmail = $Listdata2['email'];
    echo '<div id = "name_'.$usercommentid.'">';
    echo '<div><span class="name1"  >'.$usercomtname.':</span> '.'<span class ="email1">'.'['.$usercommentmail.']'.'</span>'.'</div>';
    echo '<div class = "cmnts">'.'"'.$usercomment.'"'.'</div>';
    if (Yii::app()->user->id!='' && Yii::app()->user->id!=1){
        if(Yii::app()->user->email==$usercommentmail){
           echo CHtml::ajaxButton('Delete',CHtml::normalizeUrl(array('Comments/del/id/'.$usercommentid,'render'=>true)),
                array(
                  'dataType'=>'json',
                  'type'=>'post',
                  'success'=>'function(data){
                     alert("Are You Sure?");
                     $("#name_"+data).hide();
                   }',
                ),array('id'=>$usercommentid,'class'=>'btn btn-danger'));

          }  
      }
     echo '<hr></div>';

} 
?>  

在这里你可以看到我使用了ajax删除按钮,工作正常 代码下方用于将注释保存到数据库中

<?php
    echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('Comments/create','render'=>true)),
        array(
            'dataType'=>'json',
            'type'=>'post',
            'success'=>'function(data){
                $("#AjaxLoader").hide();
                if(data.status == "success"){
                  $("#formResult").html("Comment Submitted");
                  $("#formResult").css({"color":"red"});
                  $("#comments-form")[0].reset();
                 }else{
                   $.each(data, function(key, val){
                   $("#comments-form #"+key+"_em_").text(val);
                   $("#comments-form #"+key+"_em_").show();
             });
                }
            }',
            'beforeSend'=>'function(){
            $("#AjaxLoader").show();
            }'
            ),array('id'=>'submit'.uniqid(),'class'=>'btn btn-success'));

?>

actioncreate

public function actionCreate()
{
    $model=new Comments;

    // Uncomment the following line if AJAX validation is needed
    $this->performAjaxValidation($model);

    if(isset($_POST['Comments']))
    {
        $model->attributes=$_POST['Comments'];
        // echo '<pre>';print_r($model->attributes);die();
        $valid = $model->validate();

        if($valid){
            if($model->save()){
            echo CJSON::encode(array(
                'status'=>'success'
                ));
        }
        }else{
            $error =CActiveForm::validate($model);
            if($error != '[]')
                echo $error;
            Yii::app()->end();
        }
    }
    // $this->render('create',array(
    //  'model'=>$model,
    // ));
}

0 个答案:

没有答案