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've created a tiny AJAX plugin to count hits on my articles and bypass caching but it just isn't working and not throwing up any errors.

Can you see what I am doing wrong here?

<?php
add_action('wp_ajax_nopriv_LogHit_callback', 'LogHit_callback');
add_action('wp_ajax_LogHit_callback', 'LogHit_callback');

function HitCount() {
  ?>
  <script type="text/javascript" >
  jQuery(document).ready(function($) {
    var data = {
        action: 'LogHit_callback',
        PostId:'<?php echo get_the_ID() ?>'
    };
    jQuery.post('http://www.test.com/wp-admin/admin-ajax.php', data, function( r ) {
alert( r );
  });
 </script>
 <?php


  function LogHit_callback() {
    global $wpdb; // this is how you get access to the database

    $postId = $_POST['PostId'];
    $hits = get_post_meta((int)$postId, 'hit-counter', true);
    $hits = $hits + 1;
    update_post_meta((int)$postId, 'hit-counter', (int)$hits);
    return $postId;
    exit; // this is required to return a proper result
  }
 }

It writes the following into my HTML where I call the HitCount function

 <script type="text/javascript" >

 jQuery(document).ready(function($) {
   var data = {
    action: 'LogHit_callback',
    PostId:'86505'
};
jQuery.ajax('http://www.lazygamer.net/wp-admin/admin-ajax.php', data, function( r )      {
     alert( r );
  });

 </script>

I know hard coding the URL to admin-ajax is bad but I am trying to rule out issues. if I put an alert inside the ajax call that is written to the html it works so I know that is being called.

But the second alert in the plugin file never gets called?

share|improve this question
    
closing } missing after alert( r ); –  Milo Jan 11 '13 at 5:34
add comment

1 Answer

up vote 0 down vote accepted

There is no PHP function alert, that's why your function throws an error and doesn't proceed with the rest of your code. You have to return something from the LogHit_callback function using return (or echo it with echo) and then alert it with JavaScript, that is add following code to your post function:

jQuery.post( 'http://www.test.com/wp-admin/admin-ajax.php', data, function( r ) {
    alert( r );
});

I believe that this is the correct syntax for post(). Remember that you can always debug AJAX requests with browser's developer tools. You will then know exactly what's going on.

share|improve this answer
    
I can't find the ajax request using Chromes developer tools.. what should I be looking for? I changed the alert/return issue and updated the main post but still nothing –  Gavin Mannion Jan 11 '13 at 5:05
    
Just saw an error now "Uncaught SyntaxError: Unexpected token ) " after my return –  Gavin Mannion Jan 11 '13 at 5:10
    
There should be a comma after <code>data</code> and before <code>function</code> in your JavaScript. Check my example. Just copy the code from my example, you have syntax errors all over the place. –  Mateusz Hajdziony Jan 11 '13 at 5:21
    
Sorry bad editing on my side, the comma existed in the actual code –  Gavin Mannion Jan 11 '13 at 5:26
    
But then you haven't included the closing bracket after alert( r );, that's why you have a syntax error. –  Mateusz Hajdziony Jan 11 '13 at 5:35
show 2 more comments

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.