views:

118

answers:

5

I have a link, which links to domain.com , when a person clicks, I want it to do an ajax call to counter.php and post 2 variables to it, so it can add 1 to the views for that link.

I have a link:

<a href="http://www.domain.com" onClick="addHit('12345', '1')" rel="nofollow" target="_blank">Link Title</a>

How would I do this with jquery?

EDIT:

I tried something like this

function addHit(str, partNumber){
$.get("counter.php", { version: str, part: partNumber })                
}

It seems to work, but in firebug, the request never completes... it just does that "working..." animation. counter.php echos out some text when its done (doesnt need to show up anywhere).

+1  A: 

From the jQuery documentation: http://api.jquery.com/jQuery.ajax/

function addHit(data1, data2)
{
    $.ajax({
       type: "POST",
       url: "http://domain.com/counter.php",
       data: "var1=data1&var2=data2",
       success: function(msg){
         alert( "Data Saved: " + msg ); //Anything you want
       }
     });
}
Dustin Laine
Im looking at this thru firebug, and it never "completes" the requests, it just has a working.... animation, as if its hanging.
Yegor
Try accessing your PHP directly and see the result.
Dustin Laine
A: 

I just asked a similar question and had it answered:

http://stackoverflow.com/questions/2495692/how-to-run-jquery-onclick-need-to-pass-a-variable-to-run-ajax

tylerpenney
A: 

You need to add a callback on success

function addHit(str, partNumber){
$.get(
"counter.php", 
{ 
 version: str, 
 part: partNumber 
},
function(data){
   alert("Data Loaded: " + data);
 })
)};
lemon
There is a success callback, and it works. Yet its still doing the spinning dance.
Yegor
lemon
A: 

In the case of an anchor, you're leaving the page, so firebug's going to show some weird behavior here as it thinks execution would stop. Unless you're also preventing the default event behavior of the anchor...you're leaving the page and the request (in firebug's view) is discarded.

Nick Craver
A: 

try hitting the URL through the browser ( w/o Firebug ) and see if it ever comes back a result

George
It does, its fine.
Yegor