I had created custom Post Type call smm_newsletter, and used that Post Type to create newsletter template and send newsletter email. Then, I made a meta-box for sending button include in that Post Type’s post-new.php/post.php.

This is code for that button

// I do not use type="submit" because publish/update button use it.
<input id="smm_newsletter_send" name="smm_newsletter_send" type="button" value="Send Newsletter">

and ajax

/* Newsletter Section */
var wpajax_url = smm_priv_script.admin_url + 'admin-ajax.php';
var sendingNewsletter_url = wpajax_url + '?action=smm_newsletter_sender';
// out put form console: http://localhost/wordpressProject/wordpress/wp-admin/admin-ajax.php?action=smm_newsletter_sender
console.log(sendingNewsletter_url);
$( '#smm_newsletter_send' ).on( 'click', function( e ){
    e.preventDefault();
    $form = $( 'form#post' );
    // setup our form data for ajax post
    var form_data = $form.serialize();
    console.log( form_data ); // here it work find
    // submit form data with ajax post
    $.ajax({
        'method' : 'post',
        'url' : sendingNewsletter_url,
        'data' : form_data,
        'dataType' : 'json',
        'catch' : false,
        'success' : function( data, textStatus ){
            if( data.status == 1 ) {
                // success
                // notify the user of success
                window.location.reload( true );
                console.log( data.status + 'n' );
                console.log( data.message + 'n' + '——————————' );
            } else {
                // error
                // begin building our error message text
                console.log( data.status + ' ' + 'error' + 'n' );
                console.log( data.message + 'n' + '——————————' );
            }
        },
        'error' : function( jqXHR, textStatus, errorThrown ) {
            // ajax didn't work
            console.log('A jQuery Ajax error has occurred! See details below...');
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
    // stop the form from submitting nornally
    return false;
});

But, after I clicked that button ajax() only console.log undefined for me

undefined error
undefined
——————————

I don’t know why the button cannot use. So, I tried another method to test my sending newsletter function [smm_newsletter_sender()] to make sure that function works, by changed $_POST to $_GET. And simply placed URL that look like this http://localhost/wordpressProject/wordpress/wp-admin/admin-ajax.php?action=smm_newsletter_sender?post_ID=123. OK, it work perfectly. Newsletter email sent properly. My email sending function is work find. At first, I suspected about ajax(), may I did something wrong about it? But I did not found anything wrong. So, may be It about WordPress itself that prevent something that I don’t know. Please help I have on idea now. T_T

Thank in advance,
😀

Read more here: I try to create meta-box that include ajax button on post.php, but it doesn’t work. Why?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: