WordPress.org

Ready to get started?Download WordPress

Forums

Problem with saving more checkboxes in theme options (1 post)

  1. Aboobacker P
    Member
    Posted 1 year ago #

    I'm creating some theme options and settings using ajax. Everything was working fine. But now when I'm trying to add more check box options they won't be saving. Please have a look in the following code, save it as a php file in your theme directory and include in your theme fucntions. Then access thru Appearence-> Creativ3D options.

    <?php
    add_action('admin_menu', 'creative_add_theme_page');
    
    function creative_add_theme_page() {
    	if ( isset( $_GET['page'] ) && $_GET['page'] == basename(__FILE__) ) {
    
    		add_action('admin_head', 'creative_theme_page_head');
    	}
    	add_theme_page(__('Creativ3D Options'), __('<strong style="color:#ff0000;">Creativ3D</strong> Options'), 'edit_theme_options', basename(__FILE__), 'creative_settings_page');
    }
    
    function creative_theme_page_head() {
    ?>
    <script type="text/javascript">
    	jQuery(document).ready(function($) {
    
    	  jQuery('form#creative_form').submit(function() {
    		  var data = jQuery(this).serialize();
    		  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('Creativ3D Options saved.'); ?></strong></p></div>').show();
    		} else {
    			jQuery('#saved').html('<div id="message" class="error fade"><p><strong><?php _e('Creativ3D Options could not be saved.'); ?></strong></p></div>').show();
    		}
    	}
    
    	function fade_message() {
    		jQuery('#saved').fadeOut(1000);
    		clearTimeout(t);
    	}
    </script>
    
    <?php
    }
    
    function creative_settings_page() {
    ?>
    
    <div class="wrap">
    	<h2 style="margin-bottom:10px;"><span style="color:#ff0000;">Creativ3D</span> Options</h2>
    
        <div id="saved"></div>
        <?php $options = get_option('creative_theme'); ?>
        <form action="/" name="creative_form" id="creative_form">
    
        <table class="form-table">
            <tr valign="top">
            <th scope="row">Call Us text and phone #: </th>
            <td><input type="text" name="creative_phone" value="<?php echo $options['creative_phone']; ?>" size="110" /></td>
            </tr>
    
            <tr valign="top">
            <th scope="row">Contact Email: </th>
            <td><input type="text" name="creative_email" value="<?php echo $options['creative_email']; ?>" size="110" /></td>
            </tr>
    
            <tr valign="top">
            <th scope="row">Display logo on top: </th>
            <td><input type="checkbox" name="creative_check" <?php echo ($options['creative_check'] == 'on') ? 'checked="checked"' : ''; ?>  /></td>
            </tr>
    
            <tr valign="top">
            <th scope="row">Display the top contact form on the home page: </th>
            <td><input type="checkbox" name="top_contact" <?php echo ($options['top_contact'] == 'on') ? 'checked="checked"' : ''; ?>  /></td>
            </tr>
    
       </table>
            <input type="hidden" name="action" value="creative_theme_data_save" />
            <input type="hidden" name="security" value="<?php echo wp_create_nonce('creative-theme-data'); ?>" />
            <p class="submit"><input type="submit" value="Submit"  /></p>
        </form>
    </div>
    
    <?php
    }
    
    add_action('wp_ajax_creative_theme_data_save', 'creative_theme_save_ajax');
    
    function creative_theme_save_ajax() {
    
    	check_ajax_referer('creative-theme-data', 'security');
    
    	$data = $_POST;
    	unset($data['security'], $data['action']);
    
    	if(!is_array(get_option('creative_theme'))) {
    		$options = array();
    	} else {
    		$options = get_option('creative_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('creative_theme', $data)) {
    			die('1');
    		} else {
    			die('0');
    		}
    	} else {
    		die('1');
    	}
    }
    ?>

    Why the checkbox $options['top_contact'] not saving? Please someone assist me.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.