Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to monitor the number of times a user clicks the hyperlink assigned with a class name link_counter. I'm using jQuery and PHP to do this, for example:

    $('.link_counter').click(function() {   
    <?php 
     //Retrieve the current count from dB

    @countdata = $wpdb->get_var($wpdb->prepare( "SELECT counter FROM $mycustomtable WHERE my_user_id=%d",$user_id));

    //increment counter

    $updated_count=$countdata+1;

    //update back the dB table

    $wpdb->query($wpdb->prepare("UPDATE $mycustomtable SET counter=%d WHERE my_user_id=%d",$updated_count,$user_id));
    ?>

    });

It works well. However the only issue is when there are more than one link page having the same class. If I click on the another link on that the same page, the count would not be updated.

How do I modify my code and my query to update this count without using AJAX and not reloading the page? Thanks.

share|improve this question
1  
I don't think this is doing what you think it's doing. you're registering a click when this bit of code is printed to the page, not when the link is clicked- you can't execute php within javascript. –  Milo Jan 22 '13 at 8:02
    
Multi-post: stackoverflow.com/q/14453590, please see: meta.stackexchange.com/q/64068 –  brasofilo Jan 22 '13 at 17:20

1 Answer 1

up vote 0 down vote accepted

I agree the only way that I can do this with AJAX. I have modified my scripts to include AJAX and it is now working. Thanks.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.