Cannot update my options using wp_ajax

I have implemented wp_ajax in on my options page but the value of my options are not getting saved or updated. Here is my form:

public function email_settings_form(){
    // Set class property
    $this->options = get_option( 'email_settings_options' );       
    ?>
    <div class="wrap">
        <h2>Email Settings</h2>  
        <form id="email-settings-form" method="post" action="options.php">
            <?php
            // This prints out all hidden setting fields
            settings_fields( 'schedule_section' );
            do_settings_sections( 'email-options' );
                $other_attributes = array( 'id' => 'save-email-settings' );
                submit_button( 'Save Settings', 'primary', 'save-email-settings', true, $other_attributes );
            ?>
        </form>    
        <div id="post-listing" class="wrap"></div>        
    </div>

    <?php
}

Here are my settings:

public function email_settings_page_init(){

    // Register the 'footer_message' setting with the 'General' section
    register_setting(
        'schedule_section',                      // The name of the group of settings
        'email_settings_options'            // The name of the actual option (or setting)
        //array( $this, 'schedule_sanitize' )   // Sanitize
    );

    // Let's introduce a section to be rendered on the new options page
    add_settings_section(
        'schedule_section',                             // The ID to use for this section in attribute tags
        'Email Schedule Options',                       // The title of the section rendered to the screen
        array($this, 'bbna_schedule_section_display'),  // The function used to render the options for this section
        'email-options'                            // The ID of the page on which this section is rendered
    );

    // Define the settings field
    add_settings_field(
        'number_of_articles_to_send',                          // The ID (or the name) of the field
        'Number of Articles to Send',                               // The text used to label the field
        array($this, 'number_of_articles_to_send_display'),    // The callback function used to render the field
        'email-options',                                       // The page on which we'll be rendering this field
        'schedule_section'                                          // The section to which we're adding the setting
    );

}

Here is my callback function for the section and select box:

public function schedule_section_display(){
    ?>
        <p>This section is to configure the automated mail settings for the morning editon.</p>
    <?php
}

public function number_of_articles_to_send_display(){
  $html="<select id="exclude_days" name="email_settings_options[number_of_articles_to_send]"";
        $html .= '<option>Select the number of articles to auto send</option>';
        $html .= '<option value="5"' . selected( $this->options['number_of_articles_to_send'], '5', false) . '>5</option>';
        $html .= '<option value="6"' . selected( $this->options['number_of_articles_to_send'], '6', false) . '>6</option>';
        $html .= '<option value="7"' . selected( $this->options['number_of_articles_to_send'], '7', false) . '>7</option>';
        $html .= '<option value="8"' . selected( $this->options['number_of_articles_to_send'], '8', false) . '>8</option>';
        $html .= '<option value="9"' . selected( $this->options['number_of_articles_to_send'], '9', false) . '>9</option>';
        $html .= '<option value="10"' . selected( $this->options['number_of_articles_to_send'], '10', false) . '>10</option>';
        $html .= '<option value="11"' . selected( $this->options['number_of_articles_to_send'], '11', false) . '>11</option>';
    $html .= '</select>';

    echo $html;  
}

and here is my javascript:

(function($) {
        $(function() {
            $("#bbna-email-settings-form").submit(function() {
                data = {
                    action: 'get_email_settings_ajax_results',
                    settings_nonce: settings_vars.settings_nonce
                };

                $.post(ajaxurl, data, function (response) {
                    $('#post-listing').html(response);
                });

                return false;
            });
         });
    })(jQuery);

Now when I turn off the ajax my setting save correctly. However, when I turn ajax on they don’t save or update. I know ajax is working because I am able to successfully run queries into the wp_ajax function which is below:

public function process_email_settings_ajax(){
    if( !isset( $_POST['settings_nonce'] ) || !wp_verify_nonce($_POST['settings_nonce'], 'ettings_nonce') ){
        die('Permissions check failed');    
    }

    $downloads = get_posts(array('post_type' => 'post', 'posts_per_page' => 5));
    if( $downloads ) :
        echo '<ul>';
            foreach($downloads as $download) {
                echo '<li>' . get_the_title($download->ID) . ' - <a href="' . get_permalink($download->ID) . '">' . __('View Download', 'aad') . '</a></li>';   
            }
        echo '</ul>';
    else :
        echo '<p>' . __('No results found', 'aad') . '</p>';
    endif;

    die();
}

So How do I save and update my options?

0

Leave a Comment