/    Sign up×
Community /Pin to ProfileBookmark

Jquery sort and post order to DB

I have a question connected to updating DB with Jquery sortable.

My JS code to update sort order and send it to php file via ajax is:

[CODE]
$(“#menu-pages”).sortable({
update: function(event, ui) {
$.post(“ajax.php”, { type: “orderPages”, pages: $(‘#menu-pages’).sortable(‘serialize’) } );
}
});
[/CODE]

but it updates sort results in DB on each change.

I’d rather to could use it only after button click, so I’d like to this part of code looks more like this:

[CODE]
$(“#changeSortOrder”).click(function(event) {
event.preventDefault();
$(“#result”).html(”);
var values = $(‘#menu-pages’).sortable(‘serialize’);

$.ajax({
url: “ajax.php”,
type: “post”,
data: values,
success: function(){
alert(“success”);
$(“#result”).html(‘submitted successfully’);
},
error:function(){
alert(“failure”);
$(“#result”).html(‘there is error while submit’);
}
});
});
[/CODE]

Somehow can’t make it work. I believe my PHP code is wrong:

[code=php]
parse_str($_POST[‘pages’], $pageOrder);
foreach ($pageOrder[‘page’] as $key => $value) {
mysql_query(“UPDATE test_table SET `order` = ‘$key’ WHERE `id` = ‘$value'”) or die(mysql_error());
}
[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Annaccond spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...