0

This question already has an answer here:

I' am still searching on why am getting 0 when doing a ajax call on wordpress. I know that when a ajax call is successfull it returns 0. I' am returning some value from the ajax call and it returns 0 with it.

Here's my code Javascript

$.ajax({
    url: "/wp-admin/admin-ajax.php",
    type: 'POST',
    data:{
        action: 'registerform',
        name: 'John',
        age: '38'
    },
    success: function( data ){
        alert(data);
    }
});

PHP Function

function registerform(){
    echo $_POST['name'];
}
add_action('wp_ajax_registerform', 'registerform');
add_action('wp_ajax_nopriv_registerform', 'registerform');

Above Code returns John0

Please help?

marked as duplicate by kaiser, s_ha_dum, gmazzap, Krzysiek Dróżdż, fuxia Nov 27 '13 at 7:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Modify your registerform() function to include die() after you echo your response. Example:

function registerform(){
    echo $_POST['name'];
    die();
}
add_action('wp_ajax_registerform', 'registerform');
add_action('wp_ajax_nopriv_registerform', 'registerform');
  • I would also recommend using a prefix in your function name ( example: yourprefix_registerform() ), and using a nonce ( codex.wordpress.org/WordPress_Nonces ) along with check_ajax_referrer() to secure your form. – Rachel Baker Nov 27 '13 at 3:54
  • 1
    I just want to take the opportunity to thank you for bootstrapwp! Used it a couple of times, never did that, shame on me. Everyone else, check it out! – Nicolai Nov 27 '13 at 11:35
  • Thank you ialocin! Glad to hear you found the theme useful. I am working on an update for bootstrap 3, and hope to have it released before the end of the year. – Rachel Baker Nov 27 '13 at 13:15
  • Awesome! I'm working with a fairly customized and older version, but this might a good opportunity to transfer to the next generation. – Nicolai Nov 27 '13 at 15:32

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