Ajaxify WP 选项 - 使用 jquery 序列化和复选框的问题

发布于 2024-10-14 18:27:47 字数 4141 浏览 3 评论 0原文

我一直在为 WordPress 开发一个主题选项面板。但当我开始使用超过 1 个复选框作为选项时,我遇到了一个错误。

我的代码使用jquery序列化所有表单数据,然后将其提交到wp的ajax-url。然后我添加一个回调函数,WP 知道将数据发送到我设置的函数,以将选项保存到数据库。

当我选中复选框,点击保存并且没有问题时,它就可以工作。值被保存。但现在,如果我尝试取消选中 3 个中的 1 个或 2 个并单击“保存”,则这些框已被选中。然后在刷新时,这些框仍处于选中状态。数据库中的值仍然为“ON”。我认为这是 b/c jquery 不会序列化未选中的复选框,因此它们不会被传递到 update_option 数组。由于它们不在 update_option 数组中,因此这些键的值与数据库中当前的值保持相同。因此,没有变化。奇怪的是(至少对我来说)如果我取消选中所有 3 个测试复选框,那么它确实会正确更新。

因此,我正在寻找一种解决方法,可以使用正确的值更新选项并删除未选中的复选框值。

<?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');
    }
}

你可以在这里看到完整的代码 http://pastebin.com/BCHwsBi5

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄凉少年不暖心 2024-10-21 18:27:47

显然是 1. 事情过于复杂,2. 不理解 update_option 在更新时返回 TRUE,如果没有更改则返回 FALSE,而不是在失败时返回 FALSE。

这是更正后的 test_theme_save_ajax() 函数:

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');

}

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');

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文