Chtml::ajaxlink() That Also Launches A New Browser Window

Hi Guys

basically im trying to build an external link counter.

when a user clicks on a link a model is updated but in addition to this at the same time an external website is loaded into a new browser tab.

I can get either the model update part or the new tab working but not both at the same time.

have been trying to launch the new tab on ajax ‘success’ but im not sure if this is the path i should be taking.

any help would be much appreciated.

when an ajax link generates it automatic generate id.if not then manually add one. and trigger a jquery function on that click.




echo CHtml::ajaxLink("link", Yii::app()->createUrl('count/update'), array(

                                'type' => 'POST',

                                

                                  'update'=>'#divid',

                            ),array('class'=>'active')

                                    );



and jquery code




$(document).ready(function() {   

        $('#yt0').click(function() {  // action here}); 

});



thanks for your reply Hemc

my mistake but i forgot to mention i have multiple links on one page that i need to track.

and forgive me if im missing something but i cant see how i can do that with your solution.

Give a class to all links to track and use:


$(document).ready(function() {   

        $('.class').click(function() {  // action here}); 

});

im assuming that the above code will be used to open the new browser tab with something like window.open();

but i dont know how i would get the various url’s into that

Set the correct url in the link:


echo CHtml::link("link",array('link to be opend'), array('target'=>'_blank', 'class'=>'loggingLink'));

For logging the opening use a script:


$(document).ready(function() {   

     $('.loggingLink').click(function() { 

         $.ajax({

             'url':'count/update',

             'type':'POST',

             'data':{'link': $(this).attr('href')}

         });

     }); 

});

In this way the link will be opened in a new tab by the browser itself, without any js needed.

The ajax request triggered will log the opening, by sending an ajax request to the count/update and sending in $_POST the link opened

thanks for your help zaccaria

your way makes more sense than the way i was trying to do it but after implementing your code i cant for the life of me get it to work fully.

all of it works except the action in the controller will not run so it does not log the click in the database.

the only thing i can think of is that the path to the controller might need to be different or the ajax doesnt fire because the focus is shifted to another browser tab, but im totally guessing at that. its more likely i have stuffed up something simple.

here is my code

link




<?php echo CHtml::link($item['title'],$item['url'], array('target'=>'_blank', 'class'=>'linkCount', 'id'=>$item['id'])); ?>



js




$(document).ready(function() {   

	$('.linkCount').click(function() { 

		$.ajax({

			'url':'site/linkCount',

			'type':'POST',

			'data':{'itemID': $(this).attr('id')}

		});

	}); 

});



controller




	public function actionLinkCount()

	{

		$connection=Yii::app()->db;

		$itemID = $_POST['itemID'];

		$connection->createCommand('UPDATE tbl_item SET score = score + 1 WHERE '.$itemID.' LIKE id')->execute();

	}



sorry for the late reply, but im a new user and was blocked for a day after 3 posts.

actually i think i have a jquery conflict problem.

am trying to sort that now.

will post update when i work out what ive stuffed up

ok, thanks guys for your help, i’ve finally got it working.

after fixing up some javascript errors i had i was still getting the problem and discovered that the issue i was having was with the ajax url path.

i changed it to whats below and it worked. it looks ugly and im sure theres a better way to do it but it does work.




$(document).ready(function() {   

	$('.linkCount').click(function() { 

		$.ajax({

			'url':<?php echo "'".CController::createUrl('site/linkCount')."'"; ?>,

			'type':'POST',

			'data':{'itemID': $(this).attr('id')}

		});

	}); 

});