Chtml::ajaxlink With Yii::app()->Request->Geturl();

[size="6"]ajax inside an ajax updated region problem [/size]with multiple ajaxlinks and ajaxbuttons [solved]

i want to use CHtml::ajaxLink with Yii::app()->request->getUrl() as action + parameter, and want to pass another parameter too…


<?php echo CHtml::ajaxLink('All', Yii::app()->request->getUrl(),array('type' =>'GET','data' =>array( 'val' => 'All'),'update'=>'#student_panel_handler')); ?>

any solution?

now the link is working but not getting previous Yii::app()->request->getUrl() parameters. i want that too.

used another method like this


<?php echo CHtml::ajaxLink('All', Yii::app()->createUrl('/site/manage' ),array('data' =>array( 'name' =>  $_REQUEST['name'],'admissionnumber'=> $_REQUEST['admissionnumber'],'val' => 'All',),'dataType' => 'text','update'=>'#student_panel_handler')); ?> 

the $_REQUEST[‘name’] is displaying when echo that, but not passing to the action.

any solution?


<?php echo CHtml::ajaxLink('All', array('/site/manage' ,  'name' =>  $_REQUEST['name'],'admissionnumber'=> $_REQUEST['admissionnumber'],'val' => 'All'), 'dataType' => 'text','update'=>'#student_panel_handler')); ?> 

i think , some syntax error in this, zaccaria

this is also not working!!

Maybe you are updating other parts of the page by this ajaxLink, but not the link itself. So it will hold the same parameters as the ones that were initially rendered. :)

I would render some hidden elements in the part that is to be updated by ajax.




<div id="searchName" class="hidden"><?php echo $searchName; ?></div>



I don’t want to display the raw $_REQUEST values in the view. I think it’s dangerous and you should not do that. At the very least you have to escape them.

And read the values of them when I call ajax link.




<?php

echo CHtml::ajaxLink('All',

    Yii::app()->createUrl('/site/manage' ),

    array(

        'data' => 'js:{ "searchName" : $("#searchName").html(), "val" : "All" }',

        'dataType' => 'text',

        'update'=>'#student_panel_handler'

    )

);

?> 



Not tested, and may contain syntax errors. Just a rough sketch.

yes that is right…i want to use ajax call repeatdly…want to refresh the same again and again…

i got a solution from here here

http://www.yiiframework.com/wiki/178/how-to-avoid-multiple-ajax-request/

but when i am adding id to it, its not working…

Well, my suggestion was in the assumption that your ajaxLink is located outside the ajax updated part.

It will not work if it is inside the ajax updated part.

yes it is inside

found the problem is

when we click on that link the previous link is working

is there any solution?

assume we have 6 different ajaxlinks, when we click on any one of this the same div is refreshing and echos new 6 ajaxlinks… but when we click on that actually the previous link is working…




if(isset($_REQUEST['name']) and $_REQUEST['name']!=NULL)

{

echo CHtml::ajaxLink("name", Yii::app()->createUrl('/site/manage' ), array('type' =>'GET','data' =>array( 'name' => $name,'admissionnumber'=>'','Students[batch_id]'=>$bat ),'dataType' => 'text',  'update' =>'#student_panel_handler',array()));


}


if(isset($_REQUEST['admissionnumber']) and $_REQUEST['admissionnumber']!=NULL)

{

echo CHtml::ajaxLink("add.No", Yii::app()->createUrl('/site/manage' ), array('type' =>'GET','data' =>array( 'name' => $name,'admissionnumber'=>'','Students[batch_id]'=>$bat ),'dataType' => 'text',  'update' =>'#student_panel_handler',array()));


}


if(isset($_REQUEST['Students']['batch_id']) and $_REQUEST['Students']['batch_id']!=NULL)

{

echo CHtml::ajaxLink("Batch", Yii::app()->createUrl('/site/manage' ), array('type' =>'GET','data' =>array( 'name' => $name,'admissionnumber'=>'','Students[batch_id]'=>$bat ),'dataType' => 'text',  'update' =>'#student_panel_handler',array()));


}










<?php echo CHtml::ajaxLink('A', Yii::app()->createUrl('/site/manage' ),array('data' =>array('name' =>$name,'admissionnumber'=>$ad,'Students[batch_id]'=>$bat, 'val' => 'A'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 


<?php echo CHtml::ajaxLink('B', Yii::app()->createUrl('/site/manage' ),array('data' =>array('name' =>$name,'admissionnumber'=>$ad,'Students[batch_id]'=>$bat, 'val' => 'B'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 


<?php echo CHtml::ajaxLink('C', Yii::app()->createUrl('/site/manage' ),array('data' =>array('name' =>$name,'admissionnumber'=>$ad,'Students[batch_id]'=>$bat, 'val' => 'C'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 


<?php echo CHtml::ajaxLink('D', Yii::app()->createUrl('/site/manage' ),array('data' =>array('name' =>$name,'admissionnumber'=>$ad,'Students[batch_id]'=>$bat, 'val' => 'C'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 



the first 3 links are active only after some submit.

after that when we click on that "name" link the link "A" with previous values are working…

like that

when we click on that "add.No" link the link "B" with previous values are working…

when we click on that "Batch" link the link "C" with previous values are working…

this is the problem. the link shown is correct but working is previous one.

is there any solution using like

‘beforeSend’ => ‘function(){document.body.innerHTML = “”;}’

its not ok…like this anything?

Well, as a general rule, I will not use CHtml::ajaxSomething in the div which will be updated by ajax. In other word, I only render plain html code inside an ajax updated region. And I will attach an event handler using jQuery.on().

Something like the following:




<div id="ajax-updated-region">

<?php echo CHtml::link("name", array('my/route', 'xxx' => $xxx, 'yyy' => $yyy)); ?>

<?php echo CHtml::link("number", array('my/route', 'xxx' => $xxx, 'zzz' => $zzz)); ?>

</div>


<?php

Yii::app()->clientScript->registerScript('ajax-link-handler', "

$('body').on('click', '#ajax-updated-region a', function(event){

	$.ajax({

		'type':'get',

		'url':$(this).attr('href'),

		'dataType': 'html',

		'success':function(data){

			$('#ajax-updated-region').html(data);

		}

	});

	event.preventDefault();

});

");

?>



This jQuery.on() handler watches inside the region to catch the click event. But because it resides outside of the ajax updated region (it is attached to ‘body’), it will not be damaged by the ajax updating.

Personally I believe this is the most clean way to handle an ajax inside an ajax updated region.

thanks softark once again …i will try it and return to u

sorry softark …this also not working fine . not taking the event handler… directly going to that link not ajax.

the page is render like this


$this->renderPartial('explorer',array(),false,true);

is that is the problem?

Would you please try just:




$this->renderPartial('explorer',array());



Both in the initial rendering of the page and in the ajax response.

And also check your javascript console if there’s an error.

Ah, if you do not render the ajax updated region for the first time (if you do not call renderPartial to render the partial view initially), then you have to register the javascript in your main view script.

not working…

directly going to that link

and in console error showing

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.

Would you please post your code (preferably an abbreviated version)?