2013-03-12 42 views
0

還有就是我在下面的代碼:(Yii框架)發送Ajax的形式,幾個時間

查看形式:view.php

<div class="form"> 
<?php 
    $form = $this->beginWidget('CActiveForm',array(
     'id'=>'user-profile', 
     'action' => 'index.php?r=upanel/user/update', 
     'method' => 'post', 
    )); 
?> 
    <?php $form->errorSummary($model) ?> 
    . . . 
    . . . 
    . . . 
    . . . 
    . . . 
    <div class="row buttons"> 
     <?php 
      $url = Yii::app()->createUrl('upanel/user/update'); 
      echo CHtml::ajaxSubmitButton('Update Profile',$url,array(
       'type'=>'POST', 
       'data'=> "js:$('#user-profile').serialize()",//var data = $('#user-form').serialize(); 
       'datatype'=>'html', 
       'success'=>'callback', 
       'error'=>'error', 
      )); 
     ?> 
    </div> 

<?php $this->endWidget() ?> 
</div> <!-- End CActiveForm--> 

<p><?php echo Yii::app()->user->getFlash('success') ?></p> 

<script> 
function callback(data,status){ 
    if (status=='success') 
      $('#user-profile').html(data); 
} 

function error(jqXHR, textStatus , errorThrown){ 
    console.log(jqXHR); 
    $('#error').html('callback error'); 
} 
</script> 

actionUpdate在控制器:actionUpdate()

public function actionUpdate() 
{ 
    $model=$this->loadModel(Yii::app()->user->id); 
    $model->scenario = 'updateProfile'; 

    if(isset($_POST['User'])){ 
     $model->attributes=$_POST['User']; 
     if($model->validate()){ 
      if ($model->save()){ 
        Yii::app()->user->setFlash('success','Successfully Updated'); 
        $this->renderPartial('view',array('model'=>$model)); 
      } 
     } 
    } 
} 

問題是:當表格第一次發送時,print_r($_REQUEST)顯示已發送的數據表單(在後處理方法中),但在此之後view.phppartialrender()actionUpdate,print_r($_REQUEST)表示儘管表單已填充,但發送到服務器的表單中沒有數據。 ?

回答

0

解決。只是一個笨拙的錯誤。

在回調函數success我應該更新divHTML元素,而不是form

下更改:

function callback(data,status){ 
    if (status=='success'){ 
      $('#user-profile').html(data); 
    } 

要:

function callback(data,status){ 
    if (status=='success'){ 
      $('#user-profile-div').html(data); 
    } 

感謝朋友們的指導。

1

使用該停止的形式進行再生和previos狀態更新用:

$this->renderPartial('view',array('model'=>$model),false,true); 

注中的RenderPartial方法添加的false,true

2

請注意處理ajax按鈕時的這些要點,Yii中的鏈接。

1.當使用了CHtml :: ajaxSubmitButton或了CHtml :: AjaxLink使用'live'=>false,應該有一個唯一的ID:

array('id'=>'uniqueId', 'live'=>false) 

2.如果是一個AJAX請求,使用

$this->renderPartial('_view', array(
        'model'=> $model, 
       ), false, true); 

3.如果這是一個正常的請求使用

$this->renderPartial('_view', array(
        'model'=> $model, 
      ), false, false); 

4.分配給你的ajax鏈接或按鈕一個隨機的ID

CHtml::ajaxLink('send a message', '/message', 
    array('replace' => '#message-div'), 
    array('id' => 'send-link-'.uniqid()) 
); 

Useful Link

+0

我這樣做,但沒有效果。我正在嘗試調試程序,但會等待指導的朋友。 – msoa 2013-03-12 08:10:09