I was happy to find out that Yii's CHtml::link() function already has everything needed. In particular, just adding 'submit'=>true in the $htmlOptions array, like in
<p><?php echo CHtml::link(
'Fix picture',
$url=CHtml::normalizeUrl(array('picture/fix','id'=>$model->id)),
array(
'submit' => $url,
'title' => 'Check real size and type and fix data base entries',
'csrf'=>true,
)
)
?></p>
is enough to produce a nice unobtrusive javascript code, like the following:
(in the body)
<p><a title="Check real size and type and fix data base entries" href="/yii/photoalbum/index.php?r=picture/fix&id=6" id="yt0">Fix picture </a></p>
(in the jquery code produced, at the end of the page)
jQuery('body').on('click','#yt0',function(){jQuery.yii.submitForm(this,'/yii/photoalbum/index.php?r=picture/fix&id=6',{'YII_CSRF_TOKEN':'168375210910867a3dfab3db8750c4ad5f5aee2a'});return false;});
I needed to set csrf to true because my main config file has CRSF protection enabled.I wanted the application to work even if javascript is disabled (Progressive Enhancement), so I prepared a fix.php view anyway, with a standard form containing only the submit button, like here:
<?php $form=$this->beginWidget('CActiveForm'); ?>
<div class="row submit">
<?php echo CHtml::submitButton('Fix'); ?>
</div>
<?php $this->endWidget(); ?>
The form won't be shown if users have their javascript enabled.
