Reputation: 1349
There is my code in following: (Yii framework)
View form: 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 in controller: 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));
}
}
}
}
Problem is: when the form is sent first time, print_r($_REQUEST)
show the data form been sent(in post method), but after that view.php
rendered with partialrender()
in actionUpdate
, print_r($_REQUEST)
show there is no data in the sent form to the server despite the form is filled. ?
Upvotes: 0
Views: 779
Reputation: 1349
Solved. Just only a clumsy mistake.
In callback function on success
i should have updated div
HTML element instead form
.
Changing from:
function callback(data,status){
if (status=='success'){
$('#user-profile').html(data);
}
To:
function callback(data,status){
if (status=='success'){
$('#user-profile-div').html(data);
}
Thanks friends for guidance.
Upvotes: 0
Reputation: 3950
Please Take care these points when handling ajax button,link in Yii.
1.when using CHtml::ajaxSubmitButton or CHtml::AjaxLink use 'live'=>false
and should have an unique id:
array('id'=>'uniqueId', 'live'=>false)
2.If it is an AJAX request, Use
$this->renderPartial('_view', array(
'model'=> $model,
), false, true);
3.if it is a normal request Use
$this->renderPartial('_view', array(
'model'=> $model,
), false, false);
4.Assign to your ajax link or button a random ID
CHtml::ajaxLink('send a message', '/message',
array('replace' => '#message-div'),
array('id' => 'send-link-'.uniqid())
);
Upvotes: 2
Reputation: 1199
Use this to stop the form being regenerated and updation on previos state:
$this->renderPartial('view',array('model'=>$model),false,true);
Note the false,true
added in renderPartial method.
Upvotes: 1