【问题标题】:Ajaxify WP Options - Problems with using jquery serialize and checkboxesAjaxify WP 选项 - 使用 jquery 序列化和复选框的问题
【发布时间】:2011-01-25 11:58:50
【问题描述】:

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

我的代码使用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

【问题讨论】:

    标签: wordpress themes


    【解决方案1】:

    显然是 1. 过于复杂的事情和 2. 不理解 update_option 在更新时返回 TRUE 并在没有更改而不是失败时返回 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');
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      相关资源
      最近更新 更多