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

in a view i have


<div class="panel" id="explorer_handler"></div>


echo CHtml::ajaxLink(

	'Test request',          // the link body (it will NOT be HTML-encoded.)

	array('/site/explorer'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.

	array(

		'update'=>'#explorer_handler'

	)

);

in that controller


public function actionExplorer()

	{

		

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

	}

in explorer view i have the above links




<div class="panel" id="student_panel_handler">

   <?php .

.

.

.

.

$this->renderPartial('student_panel',array('model'=>$model,

		'list'=>$posts,

		)) ;

    ?>

        

    </div>








 

and in student_panel view




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());



and in thats action


public function actionManage()

{

.

.

.

.

.

$this->renderPartial('student_panel',array('model'=>$model,

		'list'=>$posts,

		)) ;


}

these ajax links are simply working fine with only with parameter ‘val’




?php echo CHtml::ajaxLink('A', Yii::app()->createUrl('/site/manage' ),array('data' =>array('val' => 'A'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 


<?php echo CHtml::ajaxLink('B', Yii::app()->createUrl('/site/manage' ),array('data' =>array('val' => 'B'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 


<?php echo CHtml::ajaxLink('C', Yii::app()->createUrl('/site/manage' ),array('data' =>array('val' => 'C'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 


<?php echo CHtml::ajaxLink('D', Yii::app()->createUrl('/site/manage' ),array('data' =>array('val' => 'C'),'dataType' => 'text','update'=>'#student_panel_handler'),array());



its only working because of these are the same links before update

when we pass a dynamic value , the previous value is passing…its showing the current dynamic value but passing previous one.

Well, I wanted to see the code that you have modified according to my suggestion.

Where did you register the javascript to attach the event handler?

And have you changed all of your CHtml::ajaxLink to CHtml::link?

yes i changed it in student_panel view


<?php

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

$('body').on('click', '#student_panel_handler a', function(event){

	alert(1); exit;

        $.ajax({

                'type':'get',

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

                'dataType': 'html',

                'success':function(data){

                        $('#student_panel_handler').html(data);

                }

        });

        event.preventDefault();

});

");

?>


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

{

echo CHtml::link("name", array('/site/manage', 'name' => '','admissionnumber'=>$ad,'Students[batch_id]'=>$bat));

}

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

{

echo CHtml::link("", array('/site/manage',  'name' => $name,'admissionnumber'=>'','Students[batch_id]'=>$bat));

}

.

.

.

.

echo CHtml::link("All", array('/site/manage', 'name' => $name,'admissionnumber'=>$ad,'Students[batch_id]'=>''));


echo CHtml::link("A", array('/site/manage', 'name' => $name,'admissionnumber'=>$ad,'Students[batch_id]'=>''));




the links directly going to site/manage




<?php

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

$('body').on('click', '#student_panel_handler a', function(event){

        alert(1); exit;

        $.ajax({

                'type':'get',

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

                'dataType': 'html',

                'success':function(data){

                        $('#student_panel_handler').html(data);

                }

        });

        event.preventDefault();

});

");

?>




when i put this code the ajaxbutton in that is also not working


<?php  echo CHtml::ajaxButton("Apply",CController::createUrl('/site/manage'), array(

					  

																									'data' => 'js:$("#search-form").serialize()',

																									'update' => '#student_panel_handler',

																									'type'=>'GET',

																									

																								     ),array());?>

Try putting this part in the main view script.


<?php

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

$('body').on('click', '#student_panel_handler a', function(event){

	alert(1); exit;

        $.ajax({

                'type':'get',

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

                'dataType': 'html',

                'success':function(data){

                        $('#student_panel_handler').html(data);

                }

        });

        event.preventDefault();

});

");

?>



no way !! :(

i put this and links in a different view and a new action , but its not working…

Would you please start with this simplified version?

Does it popup the alert?

In your main view




<div class="panel" id="explorer_handler"></div>

...

<?php

echo CHtml::ajaxLink(

	'Test request',

	array('/site/explorer'),

	array(

		'update'=>'#explorer_handler'

	)

);

?>

...

<?php

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

$('body').on('click', '#student_panel_handler a', function(event){

	alert('link clicked');

	event.preventDefault();

});

");

?>



In the controller


public function actionExplorer()

{

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

}



In explorer view




<div class="panel" id="student_panel_handler">

<?php

...

$this->renderPartial('student_panel',array('model'=>$model,

	'list'=>$posts,

)) ;

?>

</div>



In student_panel view




<?php

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

{

	echo CHtml::link("name", array('/site/manage'));

}

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

{

	echo CHtml::link("", array('/site/manage'));

}

...

echo CHtml::link("All", array('/site/manage'));

echo CHtml::link("A", array('/site/manage'));



In the controller




public function actionManage()

{

...

$this->renderPartial('student_panel',array('model'=>$model,

		'list'=>$posts,

	)) ;

}



that is not working softark

this code alerts




if (!Yii::app()->clientScript->isScriptregistered("ajax-link-handler"))

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

      $('body').live('click',function(){alert(1);})

");



this working but it alerts when click anywhere




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

$('body').live('click', '#student_panel_handler a', function(event){

        alert('link clicked');

        event.preventDefault();

});

");



this works on click of link




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

$('#student_panel_handler a').live('click', '#student_panel_handler a', function(event){

        alert('link clicked');

		$.ajax({

                'type':'get',

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

                'dataType': 'html',

                'success':function(data){

                        $('#student_panel_handler').show(data);exit;

                }

        });

        event.preventDefault();

});

");



hey softark

this worked perfectly




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

$('#student_panel_handler a').live('click', function(event){

        $.ajax({

                'type':'get',

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

                'dataType': 'html',

                'success':function(data){

                        $('#student_panel_handler').html(data);

                }

        });

        event.preventDefault();

});

");



[size="4"]solved[/size]


$('body').on('click', '#student_panel_handler a', function(event){

    …

is correct, but


$('body').live('click', '#student_panel_handler a', function(event){

    …

is not.

http://api.jquery.com/on/

http://api.jquery.com/live/

but this didnt work for me




$('body').on('click', '#student_panel_handler a', function(event){



but this worked




$('#student_panel_handler a').live('click', function(event){



thank u very much softark :) :) :)

i will give our app link, what we work together.




$('body').on('click', '#student_panel_handler a', function(event){



I really don’t know why this doesn’t work.




$('#student_panel_handler a').live('click', '#student_panel_handler a', function(event){



As bennouna says, this is not correct. Or, the 2nd parameter is useless.

So you may change it to …




$('#student_panel_handler a').live('click', function(event){



I never used jQuery.live(), but if it serves you well, then it must be good.

i think there are some $(document).ready scripts …thats y… anyway its working perfectly…

i think u should give unique id for your ajax link, and unbind click ajax event for last ajax link.

Yii::app()->clientScript->(‘unbindAjax’,’$("#ajaxLinkId").unbind(“click”)’)

where #ajaxLinkId are the id of your last ajax link.

and use $this->renderPartial(‘viewName’,array(),true,false);

every rendering view that have client script.

and avoid using jQuery(‘body’).on(“click”) because it hard to unbind the click trigger event.

use jQuery("#ajaxLinkId").on("click") instead of jQuery("body").

if you still wanna use jQuery(‘body’) u can use some namespace on it trigger event like

jQuery(‘body’).on(“click.namespace”,"#ajaxLinkId",function(e){$.ajax({})});

to unbind just use

Yii::app()->clientScript->(‘unbindAjax’,’$(“body”).unbind(“click.namespace”)’)

and dont forget to disabled live event:

CHtml::$liveEvent=false;

I use this for my last property management project, that have lot of ajax on master form detail and every child form has an ajax.

anyway it worked for me.

Hello.

i have problem in CHtml::ajaxLink.

<?php if($job_status[0]->job_status=="Created" || $job_status[0]->job_status=="Quoted")

{

echo "hii";

echo CHtml::ajaxLink(’<i class=\‘hi hi-play fa-2x icon-vertical-padding\’></i></br><small>Start</small>’,

Yii::app()->createUrl(’/jobMaster/StartJob’,array(‘id’=>$value->id)),

array(

‘dataType’=>‘html’,

‘success’=>'function(data){

var obj=$.parseJSON(data);

if(obj.option=="start" && obj.status=="yes")

{

alert("Job Successfully Started.");

//$("#jobBox’.$i.’").load(location.href+" #jobBox’.$i.’>*","");

$("#AllJobs").load(location.href+" #AllJobs>*","");

}}’,

),array(‘class’=>‘col-md-4 col-xs-4 text-center text-warning’)

);

}if($job_status[0]->job_status=="Started")

{

echo "hello";

echo CHtml::ajaxLink(’<i class=\‘hi hi-check fa-2x icon-vertical-padding\’></i></br><small>Complete</small>’,

Yii::app()->createUrl(’/jobMaster/CompleteJob’,array(‘id’=>$value->id)),

array(

‘dataType’=>‘html’,

‘success’=>'function(data){

var obj=$.parseJSON(data);

if(data.option=="complete" && data.status=="yes")

{

alert("Job Successfully Completed.");

$("#AllJobs").load(location.href+" #AllJobs>*","");

}}’,

),array(‘class’=>‘col-md-4 col-xs-4 text-center text-success’)

);

}

?>

In this code. when i click on start it works perfectly and call right action. and update the div with changed status and link. but when i click on complete link it also called "startjob link". it have to call "completejob" link.

But when i refresh whole page and click on complete it call right action "completejob".

Sorry i am new in Yii.

What is Problem??? :(