Update linkCounter using jQuery Example?

Hi there,

This, maybe looks kinda lazy… but can someone explain to me how i can create a linkCounter using the buildin CHtml::ajaxLink() ?. (when someone clicks the link, +1 should be written to the database (clicks field) of that link or the article that links belongs to). After adding the click to the database, the user will go the given href link.

Formaly i added addclick($id) inside the onclick parameter of a hyperlink. But in Yii we create links without writing html.

Is there a little example out here about this?

<a onclick="addClick(<?php echo $article->id; ?>)" href="http://example.com">This is the link</a>

and the addClick function… (where do i create this javascript / ajax function!?)

function addClick({

// what to put here? .. where do i have to send it?


Thank you!

Take a look here :)


Anyway I don’t think this topis is related with some Yii problem :)

Imagine a digg -lookalike- system. (article + link). Now i want to count the hits of outgoing clicks. (user presses the link and goes to that site, but before all of that, i want to add 1 click to the database).

Is there a faster/easy way then doing the fallowing? :

View: Top Javascript (placed in top of view, but is this okay? it seems to work…)

<script type="text/javascript">

    function addClick(articleId) {

       $.get("<?php echo $this->createUrl('article/addClick'); ?>", { id: articleId });




echo CHtml::link(CHtml::encode($article->title),







Controller: actionAddClick


public function actionAddClick()





    echo 'done!';



Is there a possibility to include that javascript function only when we ask for it. I do not want to write this function over and over again for each 'view' that displays the article.