Reputation: 277
I'm using ajax to send form data to a plugin action and I can't get the jQuery to link to the function. The jQuery seems to be sending the code perfectly as i can see via the headers. However the WordPress php function call isn't being actioned. Can't figure it out.
$.ajax({
url: cjdAjax.ajaxurl,
type: 'POST',
action: 'cjd_send_test_email',
data: {
'email': email,
'subject': subject,
'content': content
},
success: function( data ) {
console.log( data );
$(".test-box .spinner").hide();
$(".test-email-message").slideDown();
}
});
PHP code
wp_localize_script( 'cjd_admin_script', 'cjdAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
add_action( 'wp_ajax_cjd_send_test_email', 'cjd_send_email_test' );
add_action( 'wp_ajax_nopriv_cjd_send_test_email', 'cjd_send_email_test' );
function cjd_send_email_test(){
echo $_POST['content'];
echo $_POST['subject'];
wp_die(); // ajax call must die to avoid trailing 0 in your response
}
Upvotes: 0
Views: 90
Reputation: 6828
The action
param should be part of the data
array:
data: {
action: 'cjd_send_test_email',
email: email,
subject: subject,
content: content
},
Upvotes: 2