7

Update: I managed to get the AJAX working, but not in the click event. I updated the question to match this.

I'm trying some code I found on the page: 'AJAX in Plugins'.

However I can't get it to work, and I Don't really know where to start looking. It works when I remove the click function and just run the code on pageload.

I have a Javascript file that looks ike this:

jQuery(document).ready(function(){
    jQuery("#createremotesafe").click(function(){
        alert ('test');
        var data = {
            action: 'my_action',
            whatever: 1234
        };

        // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php

        jQuery.post(ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response);
        });
    });    
});

and somewhere in my PHP script that generates the button that should execute the Javascript (i.e. the admin page), and also the rest of the page:

add_action('wp_ajax_my_action', 'my_action_callback');
echo 'test2';
function my_action_callback() {
  global $wpdb; // this is how you get access to the database
  $whatever = $_POST['whatever'];
  $whatever += 10;
  echo $whatever;
  die(); // this is required to return a proper result
}

I get both the Alert ('test') and the echo 'test2', but not the response alert. There are no Javascript errors or anything. My code is definitely running, since I can see both 'test's, but why isn't AJAX getting any response? Does my php function disappear after page load? Maybe I can't/shouldn't use the built in wp AJAX functions?

It also doesn't show an empty alert box, just nothing happens.

5
  • Two things: 1) Is ajaxurl defined and correct? 2) Where are you seeing the echo 'test2' happen?
    – Ryan Kinal
    Commented Jul 27, 2011 at 17:15
  • 1) yes (I checked), 2) before any of the content of the admin page, just on top.
    – joon
    Commented Jul 27, 2011 at 17:26
  • 2
    You're mixing up jQuery vars, you should be using a no-conflict wrapper..
    – t31os
    Commented Jul 27, 2011 at 18:24
  • Mark, thanks, I didn't know about this. However this didn't change much, the results are the same
    – joon
    Commented Jul 28, 2011 at 12:02
  • Answer added, please comment with regard to the question i raised inside my answer.
    – t31os
    Commented Jul 28, 2011 at 13:30

1 Answer 1

11

Start from a working code base..

add_action('in_admin_header', 'my_ajax_button');

function my_ajax_button() {
    echo '<a href="#ajaxthing" class="myajax">Test</a>';
}

add_action('admin_head', 'my_action_javascript');

function my_action_javascript() {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {

    $('.myajax').click(function(){
        var data = {
            action: 'my_action',
            whatever: 1234
        };

        // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
        $.post(ajaxurl, data, function(response) {
            alert('Got this from the server: ' + response);
        });
    });


});
</script>
<?php
}

add_action('wp_ajax_my_action', 'my_action_callback');

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

     $whatever = $_POST['whatever'];

     $whatever += 10;

             echo $whatever;

     exit(); // this is required to return a proper result & exit is faster than die();
}

That's the code(with a minor adjustment) from the codex page, and that's perfectly fine for me, so i'd suggest using that as a starting point.. if that's what you initially did, at what point did it break or stop working?

NOTE: Mark is me, i use a secondary account when accessing the site from another PC(that isn't mine).

5
  • it works until I try to add the Javascript to a click button event rather than a pageload. The code is run, because I get the alert, but the Ajax function probably disappears after pageload.
    – joon
    Commented Jul 28, 2011 at 15:29
  • You were mixing up vars before, it works just fine with a click function, see my updated code above(working sample).
    – t31os
    Commented Jul 28, 2011 at 15:50
  • Seems i missed some code in the updated example, it's missing add_action( 'in_admin_header', 'my_ajax_button' ); at the top of the code, fyi.
    – t31os
    Commented Jul 28, 2011 at 16:58
  • I got it working by following your advice, thanks. It's strange though that I got both of my 'test' messages and the code didn't work, cause I can't really imagine what I'm doing differently atm.
    – joon
    Commented Jul 29, 2011 at 11:16
  • Like initially said, i think it was just a case of mixing jQuery vars, you should be fine using the no-conflict approach i've posted above(anything inside that document ready function can safely use $). Happy to have helped.. ;)
    – t31os
    Commented Jul 29, 2011 at 12:41

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.