Red Virus
Red Virus

Reputation: 1707

Serialize showing up as string format

I facing an issue that am that not sure how to solve it the right way. What happens actually when I use the code below I get a string response from the server.

What can I do so the response is a array and I can loop through and update the database.

Response

string(114) "item[]=6&item[]=18&item[]=19&item[]=20&item[]=7&item[]=24&item[]=25&item[]=26&item[]=27&item[]=8&item[]=9&item[]=5"

jQuery

$('body.pages_page_x3soft-page-tree .subsubsub .button-primary').on('click', function(){
        items = $('body.pages_page_x3soft-page-tree ul.x3Soft-parent').sortable('serialize');   
        $.post(ajaxurl, {'action': 'ajax_x3softPageTreeUpdateSortOrder', 'items': items}, function( data ){
            alert( data );
        }); 
        return false;
});

$('body.pages_page_x3soft-page-tree ul.x3Soft-parent').sortable({
axis: "y",
    containment: "parent",
    forcePlaceholderSize: true,
    items: " li",
    placeholder: "sortable-placeholder",
    revert: true,
    scroll: false,
    tolerance: 'pointer',
    cursorAt: { left: 5 },
    cursor: 'move',
    sort: function(e){
        $('body.pages_page_x3soft-page-tree .subsubsub .button-primary').removeAttr('disabled');
    },
});

PHP

add_action( 'wp_ajax_ajax_x3softPageTreeUpdateSortOrder', 'ajax_x3softPageTreeUpdateSortOrder' );
add_action( 'wp_ajax_nopriv_ajax_x3softPageTreeUpdateSortOrder', 'ajax_x3softPageTreeUpdateSortOrder' );
function ajax_x3softPageTreeUpdateSortOrder(){
    var_dump($_POST['items']);
    die();
}

HTML

<ul class="x3Soft-parent ui-sortable" id="item-0">
  <li id="item_6" class="ui-sortable-handle">Resources</li>
  <li id="item_18" class="ui-sortable-handle">Newsroom</li>
  <li id="item_19" class="ui-sortable-handle">Photo Gallery</li>
  <li id="item_20" class="ui-sortable-handle">Video Gallery</li>
  <li id="item_7" class="ui-sortable-handle">About Us</li>
  <li id="item_8" class="ui-sortable-handle">Products</li>
  <li id="item_9" class="ui-sortable-handle">Services</li>
  <li id="item_5" class="ui-sortable-handle">Home</li>
</ul>

Upvotes: 2

Views: 32

Answers (1)

Navneil Naicker
Navneil Naicker

Reputation: 3691

Replace your current PHP code with the following.

function ajax_x3softPageTreeUpdateSortOrder(){
    $params = array();
    parse_str($_POST['items'], $params);
    var_dump( $params );
    die();
}

Upvotes: 1

Related Questions