I am doing a submit with a CHtml::ajaxSubmitButton and on success a div is updated by ajax in this div it is showing a new CHtml::ajaxSubmitButton.
It all works just fine but the url of the new CHtml::ajaxSubmitButton isn’t right and i am getting the url from the CHtml::ajaxSubmitButton before the update?
I am getting photosAdmin/vote instead of photosAdmin/changevote?
After a page refresh the new CHtml::ajaxSubmitButton is working as it should be?
Controller
/**
* Vote by photo_id and user_id.
*/
public function actionVote()
{
$model=new PhotosAdmin;
$modelAdminvotes=new PhotosAdminvotes;
if(Yii::app()->request->isAjaxRequest) {
if(isset($_POST))
{
$modelAdminvotes->attributes=$_POST;
$modelAdminvotes->user_id = Yii::app()->user->getId();
if($modelAdminvotes->save()){
$this->renderPartial('_form', array('model'=>$model,
'modelAdminvotes'=>$modelAdminvotes));
}
}
}
}
/**
* Update vote by photo_id and user_id
*/
public function actionChangeVote()
{
$model=$this->loadModel($_POST['photo_id']);
$modelAdminvotes=$this->loadVotesModel($_POST['photo_id']);
if(Yii::app()->request->isAjaxRequest) {
if(isset($_POST))
{
$modelAdminvotes->vote= $modelAdminvotes->vote ? 0 : 1;
if($modelAdminvotes->save()){
$this->renderPartial('_form', array('model'=>$model,
'modelAdminvotes'=>$modelAdminvotes));
}
}
}
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id)
{
$model=PhotosAdmin::model()->findByPk((int)$id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
public function loadVotesModel($id)
{
$modelAdminvotes=PhotosAdminvotes::model()->find('photo_id=:photo_id AND user_id=:user_id',array(':photo_id'=>$id,':user_id'=>Yii::app()->user->getId()));
if($modelAdminvotes===null)
$modelAdminvotes=new PhotosAdminvotes;
return $modelAdminvotes;
}
view
<div id="update-photo">
<div id="admin-votes">
<center>
<div class="photo-info">
<div class="title"><?php echo $model->title; ?></div>
</div>
<div class="textstatus">
<?php echo $model->textStatus('process'); ?>
</div>
<div class="vote-comment">
<?php echo CHtml::textArea('','Optional comment....',array('maxlength'=>255,
'class'=>'comment-input',
'rows'=>1,
'onblur'=>"if(this.value == '') { this.value = 'Optional comment....'; this.rows = '1'; }",
'onfocus'=>"if(this.value == 'Optional comment....') { this.value = ''; this.rows = '4'; }")); ?>
</div>
<?php if(!isset($modelAdminvotes->vote) || $modelAdminvotes->user_id != Yii::app()->user->getId()){ ?>
<div class="vote-buttons">
<?php echo CHtml::ajaxSubmitButton('Yes',
array('photosAdmin/vote'),
array('data'=> array(
'photo_id'=>$model->id,
'vote'=>1),
'update'=>'#content'),
array('class'=>'posvote')); ?>
<?php echo CHtml::ajaxSubmitButton('No',
array('photosAdmin/vote'),
array('data'=> array(
'photo_id'=>$model->id,
'vote'=>0),
'update'=>'#content'),
array('class'=>'negvote')); ?>
</div>
<?php } else { ?>
<div class="vote-result">
<?php var_dump($modelAdminvotes); ?>
Your vote: <?php echo CHtml::ajaxSubmitButton($modelAdminvotes->textVote(),
array('photosAdmin/changevote'),
array('data'=> array(
'photo_id'=>$model->id,
'vote'=>$modelAdminvotes->vote),
'update'=>'#content'),
array('class'=>'changevote',
'style'=>$modelAdminvotes->vote ? 'color:green' : 'color:red')); ?>
</div>
<?php } ?>
</div>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'update-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary(array($model)); ?>
<div class="row">
<?php echo $form->labelEx($model,'catid'); ?>
<?php echo $form->dropDownList($model,'catid',
$model->categoriesList(),
array('empty'=>'(Select a category)')); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord && $modelTags->isNewRecord ? 'Update' : 'Update'); ?>
<?php echo CHtml::ajaxSubmitButton('Remove',
array('delete&id='.$model->id),
array('update'=>'.container')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
</center>
</div>