• Hi,

    I’m trying to use jQuery UI Sortable to sort an array, where the old array is the current option and the new array needs to update that option. Here’s my code:

    function sortableScriptsandStyle() { ?>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
    <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
        <style>
        #sortable { list-style-type: none; margin: 0; padding: 0; width: 700px;}
        #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
        #sortable li span { position: absolute; margin-left: -1.3em; }
        </style>
        <script>
        jQuery(function($) {
    		var data = {};
            $("#sortable").sortable({
    			stop : function(event, ui){
    				$('#neworder').val($(this).sortable('toArray'));
    				$.post( ajaxurl, { action: 'save_new_order', order: $('#neworder').val()}, function( response ) {
    				   alert(response);
    				});
    			}
    		});
            $( "#sortable" ).disableSelection();
        });
        </script>
    <?php
    }
    add_action('admin_head', 'sortableScriptsandStyle');
    
    function ajax_save_new_order() {
    	$order = $_POST['order'];
    	$oldorder = get_option('sectionorder');
        foreach(explode(",", $order) as $val){
    		$neworder[] = $oldorder[$val];
    	}
    	update_option('sectionorder', $neworder);
    	print_r($neworder);
    	die();
    }
    add_action('wp_ajax_save_new_order', 'ajax_save_new_order');

    The sortable contains a list of all the titles are items in the array, assigned an ID from 0 to the number of items in the array. The jQuery takes the new order of the ID’s and sends them to the ajax_save_new_order function using Ajax. Almost everything works as expected, at the moment the code returns the neworder and an alert shows up as expected.

    Basically, everything but the update_option bit works. Does anyone have any clue why?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Update Option Ajax Not Working’ is closed to new replies.