我面临一个问题,那就是不确定如何以正确的方式解决问题。实际上,当我使用下面的代码时,我会从服务器获得字符串响应。
我能做什么,所以响应是一个数组,我可以遍历并更新数据库。
响应
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>
答案 0 :(得分:1)
使用以下内容替换当前的PHP代码。
function ajax_x3softPageTreeUpdateSortOrder(){
$params = array();
parse_str($_POST['items'], $params);
var_dump( $params );
die();
}