• Hi there, I have made a option panel for WP and can save it via the submit button. Now i like it to save via ajax, but its not working.

    First i have a file that holds the options array.
    Then i have a function that includes the option-array file.

    Then i have a optionpanel file that creates the option panel form, buttons, styles and scripts, save, update, delete functions for use in the backend.

    Lets show some code:
    On the bottom of the options form i have these lines for submitting:

    <div class="no-js-submit">
    	<?php wp_nonce_field( 'cp_action', 'cp_save_data_nonce' ); ?>
    	<?php submit_button("Save", 'primary submit-cp-button', 'submit', false ); ?>
    </div>

    Then how i save it:

    if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == $cp_page_name )
    {
    	// Save data
    	if ( isset( $_REQUEST['cp_save_data_nonce'] ) )
    	{
    		if ( check_admin_referer( 'cp_action', 'cp_save_data_nonce' ) )
    		{
    			cahe_panel_save_data();
    			header("Location: themes.php?page=$cp_page_name&data_saved=true");
    		}
    	}
    }

    Then the saving function:

    function cahe_panel_save_data()
    	{
    	// load options file
    	load_cahepanel_options();
    	global $cp_options_array;
    	// put string in database that cahepanel is configured.
    	cahe_update_option(cahe_get_theme_info()->theme_shortname . '_cahepanel_configured', 'true');
    	// Voor elke optie in cp_options_array
    	foreach($cp_options_array as $value)
    		{
    		/************************************************************************/
    		// is het id aanwezig? Anders is het geen optie
    		if (isset($value['id']))
    			{
    			// is er postdata voor betreffende id? voor die klote checkboxes
    			if (isset($_POST[$value['id']]))
    				{
    				/************************************************************************/
    				// gaat het om een upload box?
    				if ($value['type'] == 'upload')
    					{
    					// Opslaan bij idnaam met de gevalideerde post waarde.
    					// http://codex.wordpress.org/Function_Reference/esc_url_raw
    					cahe_update_option($value['id'], esc_url_raw(stripslashes($_POST[$value['id']])));
    					}
    				/************************************************************************/
    				// gaat het om een selextbox?
    				elseif ($value['type'] == 'select')
    					{
    					// opslaan bij idnaam met de sanitized post waarde.
    					cahe_update_option($value['id'], sanitize_text_field(stripslashes($_POST[$value['id']])));
    					}
    				/************************************************************************/
    				// gaat het om een enkele checkbox?
    				elseif ($value['type'] == 'single_checkbox')
    					{
    					// een checkbox geeft on, ik gebruik hier true.
    					cahe_update_option($value['id'], 'true');
    					}
    				/************************************************************************/
    				// gaat het om een enkele checkbox?
    				elseif ($value['type'] == 'slider_single_checkbox')
    					{
    					// een checkbox geeft on, ik gebruik hier true.
    					cahe_update_option($value['id'], 'true');
    					}
    				/************************************************************************/
    				// gaat het om meerdere checkboxjes?
    				elseif ($value['type'] == 'multiple_checkbox')
    					{
    					// dan de betreffende optie opslaan maar dan met arraymap. Met stripslashes_deep
    					// http://codex.wordpress.org/Function_Reference/stripslashes_deep
    					cahe_update_option($value['id'], array_map('wp_strip_all_tags', stripslashes_deep($_POST[$value['id']])));
    					}
    				/************************************************************************/
    				// gaat het om meerdere checkboxjes?
    				elseif ($value['type'] == 'slider_multiple_checkbox')
    					{
    					// dan de betreffende optie opslaan maar dan met arraymap. Met stripslashes_deep
    					// http://codex.wordpress.org/Function_Reference/stripslashes_deep
    					cahe_update_option($value['id'], array_map('wp_strip_all_tags', stripslashes_deep($_POST[$value['id']])));
    					}
    				/************************************************************************/
    				// gaat het om een text box?
    				elseif ($value['type'] == 'text')
    					{
    					// is er een validatie type in de array aanwezig?
    					if (isset($value['val_type']))
    						{
    						// is de val_type een nummer?
    						if ($value['val_type'] == 'number')
    							{
    							// sla de waarde op als een integer
    							// http://php.net/manual/en/function.intval.php
    							cahe_update_option($value['id'], intval(stripslashes($_POST[$value['id']])));
    							}
    						// is de val_type een url?
    						elseif ($value['val_type'] == 'url')
    							{
    							// zorg er dan voor dat het zeker weten een url is.
    							cahe_update_option($value['id'], esc_url_raw(stripslashes($_POST[$value['id']])));
    							}
    						}
    					// het hoeft niet gevalideerd te worden op een manier bepaald in de array
    					  else
    						{
    						// dan wel zorgen dat er geen malfide code in de db gestopt dan worden.
    						// http://codex.wordpress.org/Function_Reference/wp_kses_post
    						cahe_update_option($value['id'], wp_kses_post(stripslashes($_POST[$value['id']])));
    						}
    					}
    				/************************************************************************/
    				// gaat het om een text area?
    				elseif ($value['type'] == 'textarea')
    					{
    					// is er een val_type voor betreffende optie?
    					if (isset($value['val_type']))
    						{
    						// echo 'Er is een valtype voor deze textarea aanwezig in de array.<br/>';
    						// is dat nohtml?
    						if ('nohtml' == $value['val_type'])
    							{
    							// de gebruikte input opslaan bij de betreffende id naam en deze helemaal strippen naar nohtml data
    							cahe_update_option($value['id'], wp_strip_all_tags(stripslashes($_POST[$value['id']])));
    							// echo 'De val_type heeft als waarde nohtml.<br/><br/>';
    							} // hier later eventueel meer.
    						}
    					// er is geen val_type voor betreffende optie.
    					  else
    						{
    						// Administrators mogen wel data posten die niet gestript word.
    						if (current_user_can('unfiltered_html'))
    							{
    							cahe_update_option($value['id'], stripslashes($_POST[$value['id']]));
    							}
    						// geen administrator maar wel rechten dan toch strippen voor de zekerheid.
    						  else
    							{
    							// http://codex.wordpress.org/Function_Reference/wp_filter_post_kses
    							// http://php.net/manual/en/function.addslashes.php
    							cahe_update_option($value['id'], stripslashes(wp_filter_post_kses(addslashes($_POST[$value['id']]))));
    							}
    						}
    					}
    				/************************************************************************/
    				}
    			/************************************************************************/
    			// there is no postdata for checkboxes
    			  else
    				{
    				if ($value['type'] == 'single_checkbox')
    					{
    					// give it a false
    					cahe_update_option($value['id'], 'false');
    					}
    				if ($value['type'] == 'slider_single_checkbox')
    					{
    					// give it a false
    					cahe_update_option($value['id'], 'false');
    					}
    				elseif ($value['type'] == 'multiple_checkbox')
    					{
    					// give it an empty array
    					cahe_update_option($value['id'], array());
    					}
    				elseif ($value['type'] == 'slider_multiple_checkbox')
    					{
    					// give it an empty array
    					cahe_update_option($value['id'], array());
    					}
    				  else
    					{
    					// delete option
    					cahe_delete_option($value['id']);
    					}
    				} // END -> else scenario from ( isset( $_POST[ $value['id'] ] ) )
    			/************************************************************************/
    			} // END -> ( isset( $value['id'] ) )
    		} // END --> foreach ($cahepanel_options_array as $value)
    	} // END -> cahe_panel_save_data

    This is working. Via this way it stores for every option, the validated value in the database with current option name and the theme prefix.
    So when i have 50 options it stores 50 different options in the database.

    I managed to save some things via this info:
    Link

    But when i call my saving function in that function. Is saves some data but not all and no values.. Also because i dont understand it.. How can this get my data to jQuery?
    I realy dont know where to start to save this via ajax.

    Could anyone help?
    Im looking for a way to get this working but after 4 day’s i have a big headache 🙁

The topic ‘Update Option via Ajax’ is closed to new replies.