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 have this ajax call which gets the value for catname:

jQuery('.bio a').click( function(){
    var catname = $(this).closest(".bio").data('category');
        alert(catname);
    jQuery.ajax({  
    cache: false,
        type: 'POST', 
        url: 'http://www.mysite.com/wp-admin/admin-ajax.php',  
        data: {  
            action: 'performers',
            catname: catname
            /*nonce: nonce,*/
        }
    });  
});

I have this function which updates a custom field:

function performers(){  
    $test = $_POST['catname'];  
    return $test;
    }  
add_action('wp_ajax_nopriv_ performers', 'performers');  
add_action('wp_ajax_performers', 'performers');  

I use the $test variable in single.php

Questions:

  1. Any idea why is this code not working? jquery var catname is being set (tested this with the alert). Maybe it has to do with caching? I did try to update a custom field with $test variable and retrieve the value of that custom field in single.php, still no luck.

  2. I saw a tutorial using nonce? Do you advise me to use it? How?

I have tried this, still no luck (called performers(); in single.php in order for it to work.

function performers(){  
        $test = $_POST['catname'];
        update_post_meta(758, 'perfx', $test);
    }  
    add_action('wp_ajax_nopriv_performers', 'performers');  
    add_action('wp_ajax_performers', 'performers');  
share|improve this question
1  
possible type, 2nd code block wp_ajax_nopriv_ performers should be wp_ajax_nopriv_performers (no spacces) –  janw Sep 18 '12 at 20:56
1  
What does the ajax call return? check the response in Firebug/chrome console –  janw Sep 18 '12 at 20:59
add comment

1 Answer

Instead of doing:

data: {action: 'performers', catname: catname}

Try:

data: 'action=performers&catname='+catname;

You may also want to define "catname" globally, i am not sure if scope is an issue with your ajax handler but this could be why it is not being set. As an example try to alert catname within the ajax handler above cache=false; if the alert is not thrown then catname is not set.

Instead of using alerts which can become tedious you could use console.log(catname) and review the value within the console.

In relation to using nonce for security e.t.c I can't help you there, I am sure there are a plethora of articles out there explaining its use. google it. check out the wordpress codex to.

Hope this helps.

share|improve this answer
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.