1

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;
?> 
6
  • Could you please edit the code into your question? Commented May 10, 2013 at 18:41
  • Can you post your php code segment here Commented May 10, 2013 at 18:43
  • don't do '{"order":' + JSON.stringify(dataArr) + '}' to pass the data just pass the object as var obj = {"order":dataArr} Commented May 10, 2013 at 18:45
  • added the php + javascript code to the question although I'm not certain its the right way to go about it. Commented May 10, 2013 at 19:05
  • @scrappedcola, do you mean var dataStr = {"order":dataArr};? If I don't stringify it how will I access the data of an object in php? Commented May 10, 2013 at 19:11

1 Answer 1

3

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']
Sign up to request clarification or add additional context in comments.

4 Comments

How should my php code look for handling this? I've tried $value = json_decode($_POST['orders']); $order_0=$value['order'][0]; echo $order_0; Is this what you meant or something else?
you don't have to decode anything just simply use $_POST['orders']. Try var_dump($_POST['orders']); and you will see the results. You can then display it as you like.
When using the var_dump I'm getting a NULL return, is there an error in how I'm sending the data or receiving it? Altered code -> jsfiddle.net/dHVHQ
sorry i was out for a while. Were you able to figure this out? do you still need help? If you are getting a NULL on var_dump that means whatever you are posting using ajax is null. In you javascript just before ajax call do a console.log(dataArr) and check if it is populated or not?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.