phpwordpressthemes

Ajaxify WP Options - Problems with using jquery serialize and checkboxes


i've been working on a theme options panel for wordpress. but i've run into a bug when i start using more than 1 checkbox as an option.

my code uses jquery to serialize all the form data and then submit it to wp's ajax-url. then i add a callback function and WP knows to send the data to the function i have set up to save the options to the DB.

it works when i check the boxes ON, hit save and no problem. the values are saved. but now that the boxes are checked if i try to uncheck 1 or 2 of the 3 and click save... then on refresh the boxes are still checked. the values are still 'ON' in the DB. I think this is b/c jquery doesn't serialize unchecked checkboxes, so they aren't be passed to the update_option array. since they aren't in the update_option array the values for those keys stays the same as is currently in the DB. hence, no change. strangely (to me atleast) if i uncheck all 3 of my test checkboxes then it does update properly.

so, i am looking for a work around that will update the options with the correct values and remove checkbox values that have been unchecked.

<?php

add_action('admin_menu', 'test_add_theme_page');

function test_add_theme_page() {
    if ( isset( $_GET['page'] ) && $_GET['page'] == basename(__FILE__) ) {

        add_action('admin_head', 'test_theme_page_head');
    }
    add_theme_page(__('Test Admin'), __('Test Admin'), 'edit_themes', basename(__FILE__), 'test_theme_page');
}




function test_theme_page_head() {
?>

    <script type="text/javascript">
    jQuery(document).ready(function($) {

      jQuery('form#test_form').submit(function() {
          var data = jQuery(this).serialize();

    alert(data);

          jQuery.post(ajaxurl, data, function(response) {
              if(response == 1) {
                  show_message(1);
                  t = setTimeout('fade_message()', 2000);
              } else {
                  show_message(2);
                  t = setTimeout('fade_message()', 2000);
              }
          });
          return false;
      });

    });

    function show_message(n) {
        if(n == 1) {
            jQuery('#saved').html('<div id="message" class="updated fade"><p><strong><?php _e('Options saved.'); ?></strong></p></div>').show();
        } else {
            jQuery('#saved').html('<div id="message" class="error fade"><p><strong><?php _e('Options could not be saved.'); ?></strong></p></div>').show();
        }
    }

    function fade_message() {
        jQuery('#saved').fadeOut(1000);
        clearTimeout(t);
    }
    </script>

<?php
}




function test_theme_page() {
?>

<div class="wrap">

    <h2><?php _e('Test Admin'); ?></h2>

    <div id="saved"></div>
    <?php $options = get_option('test_theme'); 

 echo "<br>";
 print_r($options);
 echo"<br>";
 ?>
    <form action="/" name="test_form" id="test_form">
        Text<input type="text" name="test_text" value="<?php echo $options['test_text']; ?>" /><br />
        Check1<input type="checkbox" name="test_check1" <?php echo ($options['test_check1'] == 'on') ? 'checked' : ''; ?> /><br />
  Check2<input type="checkbox" name="test_check2" <?php echo ($options['test_check2'] == 'on') ? 'checked' : ''; ?> /><br />
  Check3<input type="checkbox" name="test_check3" <?php echo ($options['test_check3'] == 'on') ? 'checked' : ''; ?> /><br />
        <input type="hidden" name="action" value="test_theme_data_save" />
        <input type="hidden" name="security" value="<?php echo wp_create_nonce('test-theme-data'); ?>" />
        <input type="submit" value="Submit" />
    </form>

</div>

<?php
}




add_action('wp_ajax_test_theme_data_save', 'test_theme_save_ajax');

function test_theme_save_ajax() {

    check_ajax_referer('test-theme-data', 'security');

    $data = $_POST;
    unset($data['security'], $data['action']);

    if(!is_array(get_option('test_theme'))) {
        $options = array();
    } else {
        $options = get_option('test_theme');
    }

    if(!empty($data)) {
        $diff = array_diff($options, $data);
        $diff2 = array_diff($data, $options);
        $diff = array_merge($diff, $diff2);
    } else {
        $diff = array();
    }

    if(!empty($diff)) {
        if(update_option('test_theme', $data)) {
            die('1');
        } else {
            die('0');
        }
    } else {
        die('1');
    }
}

you can see the full code here http://pastebin.com/BCHwsBi5


Solution

  • was apparently 1. over-complicating things and 2. not understanding that update_option returns TRUE on an update and returns FALSE if there is no change and NOT on failure.

    here's the corrected test_theme_save_ajax() function:

    function test_theme_save_ajax() {
    
        check_ajax_referer('test-theme-data', 'security');
    
        $data = $_POST;
        unset($data['security'], $data['action']);
    
        update_option('test_theme', $data);
        die('1');
    
    }