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.

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.

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

1 Answer

up vote 0 down vote accepted

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).

share|improve this answer
    
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 Jul 28 '11 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 Jul 28 '11 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. –  Mark Duncan Jul 28 '11 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 Jul 29 '11 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 Jul 29 '11 at 12:41
add comment

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.