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.

FIXED

I changed this:

wp_enqueue_script( 'pfxconversion', plugin_dir_url( __FILE__ ) . 'function.js', array( 'jquery' ) );
    wp_localize_script( 'pfxconversion', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

    add_action( 'wp_ajax_nopriv_pfxconversion', 'pfxconversion_callback' );
    add_action( 'wp_ajax_pfxconversion', 'pfxconversion_callback' );

to this:

add_action ('wp_print_scripts', 'widget_logic_js_fx', 1);

function widget_logic_js_fx() {
    wp_enqueue_script( 'purefx-currency-converter', plugin_dir_url( __FILE__ ) . 'function.js', array( 'jquery' ) );
    wp_localize_script( 'purefx-currency-converter', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

add_action( 'wp_ajax_nopriv_pfxconversion', 'pfxconversion' );
add_action( 'wp_ajax_pfxconversion', 'pfxconversion' );

wp_print_scripts is what we we're missing :-)

share|improve this question
    
You should post your solution as an answer and then accept that answer as the best answer when you can! Also, I would suggest accepting answers to your other questions if they have been solved, your accept rate is at 0%. –  Jared Feb 15 '12 at 3:53
    
@Jared I'll bare that in mind in future, still getting used to how everything works here :-) –  Danny Feb 15 '12 at 15:19
add comment

1 Answer

up vote 4 down vote accepted

1: the codex recommends enqueing all scripts and styles on the wp_enqueue_scripts() hook. this won't solve the problem, but is good practice.

2: i think that all the data that goes INTO the php function is wrapped up in $_POST. which is sent by the .ajax call. so it doesn't look like you are sending any data other than the action name and it doesn't look like you are properly decoding the data in the PHP file.

i'd start out changing:

function pfxconversion () {
  echo "bacon"; exit();
}

just to ensure that you are talking to the ajax function. that part looks correct to me though.

what kind of info are are sending? some form data? you'll need to serialize that and include it in the ajax call.

jQuery(document).ready(function() {

jQuery("#convert").click(function () {

     jQuery.ajax({
       type: "POST",
       url: MyAjax.ajaxurl,
       data: action='pfxconversion&bacon=hellyes',
       success: function(data){

       jQuery('#results').show();
       //Put received response into result div
       jQuery('#results').html(data);
       }
     });
});

then on the php side we can get at the post data like this:

function pfxconversion () {
      $data = $_POST;
      echo $data['bacon'];
      exit();
    }

in theory that should echo out the "hell yes" we set earlier.

you're probably sending a form in which case you'd tack on $('#formID').serialize() like so

var formdata = $('#formID').serialize();

then your data line in the ajax call would look like:

data: formdata + 'action=pfxconversion',

since you didn't post what type of info you were trying to send (the HTML) I didn't go ahead and do that, but hopefully you get the gist from my example.


EDIT:

so i took a look at admin-ajax.php and you can see 2 things. first, if the user is not logged in it will try to run

do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );

this action doesn't fire if you are logged in.... which i am on my local install where i am testing this.

so to ensure that this works for BOTH logged in and logged out users we need to add the action to both wp_ajax_ and wp_ajax_nopriv_

secondly, a zero response means that it admin-ajax.php went through all its own switch cases, and didn't find an action.

default :
    do_action( 'wp_ajax_' . $_GET['action'] );
    die('0');
    break;
endswitch;

that's the endtail of the code.... what it means for us is that it isn't finding the action name.

so with that i made some tweaks and i am now getting a response:

jQuery(document).ready(function() {     
    $("#convert").click(function () { 

            var formdata = $('#conversionform').serialize();

        $.ajax({
            type: "POST",
            url: MyAjax.ajaxurl,
            data: 'action=pfxconversion&' + formdata,
            type: 'POST',
            cache: false,
            success: function (resp) { 
                console.log(resp);
            },
            error: function (err) {
                console.log(err);
            }
        });
        return false;
    });     
});

and on the php side:

add_action( 'wp_ajax_nopriv_pfxconversion', 'pfxconversion_callback' );
add_action( 'wp_ajax_pfxconversion', 'pfxconversion_callback' );

function pfxconversion_callback () {
    print_r($_POST);
    exit();
}

that should show an array of values from the form in your console... which means you're now talking to the callback function successfully through ajax.

ps- you can add a hidden input with the action to the form.

<input type="hidden" name="action" value="pfxconversion" />

and then it'll be pulled in automatically by serialize() and you won't need to concatenate the string in the .ajax call

//edit

function add_my_scripts(){
wp_enqueue_script( 'pfxconversion', plugin_dir_url( __FILE__ ) . 'function.js', array( 'jquery' ) );
wp_localize_script( 'pfxconversion', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_enqueue_scripts','add_my_scripts');
share|improve this answer
    
Many thanks for taking the time to respond, that all makes perfect sense so i'll have a bash at implementing your recommendations. I am indeed processing form data. –  Danny Feb 13 '12 at 13:23
    
outside the ajax call –  helgatheviking Feb 13 '12 at 13:40
    
So now i'm getting -1 output and looking at firebug it's only picking up the amount and from POST but not the to. I can't for the life of me see any problem with it! Any ideas? –  Danny Feb 13 '12 at 17:04
    
you have your add_action syntax messed up. please see my edits. it should take the form: add_action( 'wp_ajax_nopriv_ACTION-NAME-IN-AJAX-CALL', 'FUNCTION-NAME-IN-PHP' ); –  helgatheviking Feb 13 '12 at 18:30
    
Many thanks again, that was a really big help and i certainly understand how Wordpress handles Ajax a lot better, however i'm still getting a -1 output, ah the joys of debugging :-) –  Danny Feb 13 '12 at 19:30
show 9 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.