Need Ajax to refresh my admin page without reloading
-
Hi there
Need help to get the admin page to refresh by the Ajax style ;o)
This is my Php function:
function l_next_top_count_callback() { global $wpdb; $next_top_count = intval($_POST['next_top_count']); echo $next_top_count; wp_die(); } add_action('wp_ajax_l_next_top_count_callback', 'l_next_top_count_callback'); add_action('wp_ajax_nopriv_l_next_top_count_callback', 'l_next_top_count_callback');
This is my jQuery ajax action:
(function($) { $(document).ready(function() { $('#next_top_count').on('click',function(e){ e.preventDefault(); var next_top_count = $("#next_top_count").val(); var data = { action: 'l_next_top_count_callback', next_top_count: next_top_count, }; $.ajax({ type: 'post', url: ajaxurl.ajax_url, data: data, success : function( response ){ console.log(data); }, error : function( response ){ alert("fejl") } }); }); }); })(jQuery);
This is my update_option function:
add_option( 'next_top_count_', ''); register_setting( 'next_top_count_group', 'next_top_count', 'next_top_count_callback' );
My wp-admin.php call:
wp_localize_script( 'ajax_count', 'ajaxurl', admin_url( 'admin-ajax.php' ) ); wp_enqueue_script( 'ajax_count' );
My code is working with the database :o)
But, my options-general.php does noget refresh without refresh f5.
Hope the Ajax will du it automatic so my for loop can do the job. here is the for loop:for ($next_top = 0; $next_top <= get_option( 'next_top_count_' ); $next_top++) { // Save attachment ID if ( isset($_POST['(hidden)_top_banner_name'.$next_top.'']) || isset($_POST['(hidden)_top_banner_name_color'.$next_top.'']) || isset($_POST['(hidden)_top_banner_name_top_height'.$next_top.'']) || isset($_POST['(hidden)_top_banner_name_top_text_line'.$next_top.'']) || isset($_POST['(hidden)_top_banner_name_top_start_date'.$next_top.'']) || isset($_POST['(hidden)_top_banner_name_top_stop_date'.$next_top.'']) || isset( $_POST['submit_image_selector'.$next_top.''] ) && isset( $_POST['image_attachment_id'.$next_top.''] ) ) : update_option( 'media_selector_attachment_id'.$next_top.'', absint( $_POST['image_attachment_id'.$next_top.''] ) ); update_option( '(hidden)_top_banner_name'.$next_top.'', wp_kses_post( $_POST['(hidden)_top_banner_name'.$next_top.''] ) ); update_option( '(hidden)_top_banner_name_color'.$next_top.'', wp_kses_post( $_POST['(hidden)_top_banner_name_color'.$next_top.''] ) ); update_option( '(hidden)_top_banner_name_top_height'.$next_top.'', wp_kses_post( $_POST['(hidden)_top_banner_name_top_height'.$next_top.''] ) ); update_option( '(hidden)_top_banner_name_top_text_line'.$next_top.'', wp_kses_post( $_POST['(hidden)_top_banner_name_top_text_line'.$next_top.''] ) ); update_option( '(hidden)_top_banner_name_top_start_date'.$next_top.'', wp_kses_post( $_POST['(hidden)_top_banner_name_top_start_date'.$next_top.''] ) ); update_option( '(hidden)_top_banner_name_top_stop_date'.$next_top.'', wp_kses_post( $_POST['(hidden)_top_banner_name_top_stop_date'.$next_top.''] ) ); endif; wp_enqueue_media(); ?> <div class="plugin_box"> <form method='post'> <div class="plugin_(hidden)_banner"> <div><label for="(hidden)_top_banner_name<?php echo $next_top; ?>"><b>Banner tekst</b></label></div> <?php settings_fields( '(hidden)_top_banner_group' ); $(hidden)_top_banner_name = get_option('(hidden)_top_banner_name'.$next_top.''); wp_editor( $(hidden)_top_banner_name, '(hidden)_top_banner_name'.$next_top.'', array( 'wpautop' => true, 'media_buttons' => true, 'textarea_rows' => 1, ) );?> </div> <style> body.settings_page_(hidden)_top_banner .wp-editor-wrap { width: 76%; float: right; } .plugin_(hidden)_top_banner { float: left; width: 22%; } .plugin_box { display: flow-root; padding-top: 1em; } .plugin_(hidden)_top_banner_dato { width: 85px; } </style> <div class="plugin_(hidden)_top_banner"> <div><label><b>Baggrundsfarve</b><br></label><input type="text" class="color-picker" data-alpha="true" name="(hidden)_top_banner_name_color<?php echo $next_top; ?>" id="(hidden)_top_banner_name_color<?php echo $next_top; ?>" value="<?php echo get_option( '(hidden)_top_banner_name_color'.$next_top.'' ); ?>" /></div> <label><b>Baggrundsbillede</b></label> <div class='image-preview-wrapper<?php echo $next_top; ?>'> <img id='image-preview<?php echo $next_top; ?>' src='<?php echo wp_get_attachment_url( get_option( 'media_selector_attachment_id'.$next_top.'' ) ); ?>' width='100%'> </div> <input id="upload_image_button<?php echo $next_top; ?>" type="button" class="button" value="<?php _e( 'Upload banner billede' ); ?>" /> <label><b>Vis billede</b></label> <input type='checkbox' name='image_attachment_id<?php echo $next_top; ?>' id='image_attachment_id<?php echo $next_top; ?>' value='<?php echo get_option( 'media_selector_attachment_id'.$next_top.'' ); ?>' checked><br><br> <div><b>Height p? banner linjen</b></div> <input type="number" maxlength="4" size="4" name="(hidden)_top_banner_name_top_height<?php echo $next_top; ?>" id="(hidden)_top_banner_name_top_height<?php echo $next_top; ?>" value="<?php echo get_option( '(hidden)_top_banner_name_top_height'.$next_top.'', '28' ) ?>"> <div><b>Tekst linje h?jde</b></div> <input type="text" size="4" name="(hidden)_top_banner_name_top_text_line<?php echo $next_top; ?>" id="(hidden)_top_banner_name_top_text_line<?php echo $next_top; ?>" value="<?php echo get_option( '(hidden)_top_banner_name_top_text_line'.$next_top.'', '1.8' ) ?>"> <div><b>Start</b></div> <input type="text" class="date_picker plugin_(hidden)_top_banner_dato" name="(hidden)_top_banner_name_top_start_date<?php echo $next_top; ?>" id="(hidden)_top_banner_name_top_start_date<?php echo $next_top; ?>" value="<?php echo get_option( '(hidden)_top_banner_name_top_start_date'.$next_top.'' ) ?>"> <div><b>Slut</b></div> <input type="text" class="date_picker plugin_(hidden)_top_banner_dato" name="(hidden)_top_banner_name_top_stop_date<?php echo $next_top; ?>" id="(hidden)_top_banner_name_top_stop_date<?php echo $next_top; ?>'" value="<?php echo get_option( '(hidden)_top_banner_name_top_stop_date'.$next_top.'' ) ?>"><br><br> <input type="submit" name="submit_image_selector<?php echo $next_top; ?>" value="Gem ?ndringer" class="button-primary"> </div> </form> </div> <hr> <?php }
All this work, but not before i have refresh the page.
Is there not way to do that with ajax, and how do i do it, have try many this but can’t get it to work, have use Google many times, can’t move on, really need some help to do this.Hope you will help.
I dont have so mush hair anymore :oD
Best Regards
MortenThe page I need help with: [log in to see the link]
- The topic ‘Need Ajax to refresh my admin page without reloading’ is closed to new replies.