Reputation: 55
The problem at its simplest is I'm using jQuery on the client side to manipulate some list items in HTML. I'm storing the list items and the order they're left in by the user in an array and trying to pass that array into PHP. I'm not sure what the best way is to do this, I've tried using json but it hasn't worked for me, that may be syntax or some other problem. If there is an easier way to do this I'd happily do that.
I'm simply trying to reach a scenario where I can echo out the contents of the array in PHP. i.e. if the user had number 12 in the 2nd position be able to echo value[2] for the number 12.
My HTML + Javascript is here. It's a simple jquery tile list and I want to access the way the user has ordered the list. http://jsbin.com/igewiv/2/edit
For the JSON PHP segment I'm not getting anything returned from the echo, again I'm happy to do this another way if its successful. Help greatly appreciated.
Javascript:
$(document).ready(function(){
$("#sortable").on("sortupdate", function() {
var dataArr = [];
$("#sortable li").each(function(idx, elem) {
dataArr[idx] = $(elem).html();
});
var dataStr = '{"order":' + JSON.stringify(dataArr) + '}';
$.ajax({
url: "fiddle.php",
type: "POST",
data: dataStr
});
alert(dataStr);
});
});
PHP:
<?php
$value = json_decode($_POST["dataStr"]);
$order_0=$value['order'][0];
echo $order_0;
?>
Upvotes: 1
Views: 127
Reputation: 5470
pass it like this:
$.ajax({
url: "fiddle.php",
type: "POST",
data: {
orders: dataArr
}
});
remove the line where you are stringifying it. Its not needed.
in the php side you should receive
$_POST['orders']
Upvotes: 3