weixin_33736832 2015-09-08 08:27 采纳率: 0%
浏览 19

Ajax更新yii中的内容

I have an issue with the ajax, which doesn't update the content which is saved. The data are saving fine in the database. It only shows when I refresh the page. The code for echo the comments

<?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>';

} 
?>  

here you can see I used ajax delete button which works fine.
Below the code is used to save the comment into the database

<?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'));

?>

And the actioncreate is

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch