0

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
}

1 Answer 1

2

The action param should be part of the data array:

data: {
    action: 'cjd_send_test_email',
    email: email, 
    subject: subject,
    content: content
},
Sign up to request clarification or add additional context in comments.

1 Comment

Knew it would be something simple ;) - Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.