0

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?

1
  • closing } missing after alert( r );
    – Milo
    Commented Jan 11, 2013 at 5:34

1 Answer 1

0

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.

7
  • 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 Commented Jan 11, 2013 at 5:05
  • Just saw an error now "Uncaught SyntaxError: Unexpected token ) " after my return Commented Jan 11, 2013 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. Commented Jan 11, 2013 at 5:21
  • Sorry bad editing on my side, the comma existed in the actual code Commented Jan 11, 2013 at 5:26
  • But then you haven't included the closing bracket after alert( r );, that's why you have a syntax error. Commented Jan 11, 2013 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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