saegeek
(Abdallah)
December 25, 2009, 10:03am
1
Hi @ all !
I would like to do an ajax request.
I saw how about making a such query in the cookbook but I’m wondering how I could tell a such code by an onclick event like in :
<input type="button" onclick="Ajax('foobar');">
Using a code like :
<?php echo CHtml::script(CHtml::ajax(array('url'=>'/ajax',
'type'=>'GET',
'dataType'=>'html',
'type'=>'POST'
'data'=>'({title : this.getAttribute(\'id\')})'
)));?>
I get :
<script type="text/javascript">
/*<![CDATA[*/
jQuery.ajax({'url':'/ajax','type':'POST','dataType':'html','data':'({title : this.getAttribute(\'id\')})','cache':false});
/*]]>*/
</script>
Should I have to use something like :
<input type="button" onclick="jQuery.ajax();">
saegeek
(Abdallah)
December 25, 2009, 3:32pm
2
After some forum search, I finally found how to do.
I had to call CHtml::ajaxLink() or CHtml::ajaxButton() functions like in :
echo CHtml::ajaxLink('#',bu().'/ajax/titles',
array(
'type'=>'POST',
'data'=>'titre=#',
'dataType'=>'html',
//'replace'=>'title',
'beforeSend' => 'function(){$("#title").replaceWith(\'<img id="title" alt="Veuillez patienter..." src="'.bu().'/images/loading.gif">\');}',
'success'=>'function(html){$("#title").replaceWith(html);}'
));
But I had to NOT use the Yii “replace” keyword because it wouldn’t replace the target HTML element (here with id=“title”) but have to use manually a ‘success’ tag.
saegeek
(Abdallah)
December 25, 2009, 3:35pm
3
oops, it was a syntax error, i found how to use the ‘replace’ tag
example for :
<input id="title">
I had to do a :
'replace'=>'#title',