0

Initial javascript to get a string/array:

<script type="text/javascript">
$(document).ready(function() {
    $("#table").tableDnD({
    onDrop: function(table, row){
        var rows = table.tBodies[0].rows;
            var debugStr = "";
            for (var i=1; i<rows.length; i++) {
                debugStr += rows[i].id+":";
                //debugStr += rows[i].id;
            }
        data = debugStr; // colon seperated
        }
    });
});
function sendData()
{
    var packed = ""; // Initialize packed or we get the word 'undefined'
    for (i = 0; (i < data.length); i++) {
    if (i > 0) {
        packed += ",";
        }
    packed += escape(data[i]);
        }
    document.data.data.value = packed;
    document.data.submit();
}
</script>

Now in the PHP...

Return my array/string from post:

$packed = $_POST['data'];
$data = split(":", $packed);
for ($i = 0; ($i < count($data)); $i++) {
  $data[$i] = rawurldecode($data[$i]);
  $data[$i] = str_replace(",", "", $data[$i]);
  $data[$i] = str_replace(":", ",", $data[$i]);
}

create an array from it:

$arrayUpdated = array();
foreach($data as $value) {
    print_r($value);
    $arrayUpdated[] = $value;
    }

which returns:

Array ( [0] => 9,8,6,5,4,3,2,15,1,0, ) 

when I was expecting:

Array ( [0] => 9, [1] => 8, [2] => 6, [3] => 5, [4] => 4, [5] => 3, [6] => 2, [7] => 15, [8] => 1, [9] => 0, ) 

what have I got wrong? Thanks, Andy

2
  • What happens if you do print_r($packed) straight after it intercepts the POST array? Commented Nov 1, 2010 at 13:35
  • er... so sendData inserts a comma after each character in data? Why? Commented Nov 1, 2010 at 15:37

2 Answers 2

2

Since you're using JQuery already, wouldn't it be easier to send the data in JSON format rather than manually building it into a colon/comma separated and then manually spliting it apart in PHP?

In your JS page, replace most of your sendData() function with a call to $.toJSON(data);:

function sendData() {
    document.data.data.value = $.toJson(data);
    document.data.submit();
}

In your PHP code, simlpy use json_decode() to load your data into an array with exactly the same structure as you had in Javascript.

$data=json_decode($_POST['data']);
print_r($data);

*Note: you may need an extra JQuery plugin to get the toJSON() method -- See http://code.google.com/p/jquery-json/ for more info.

Sign up to request clarification or add additional context in comments.

4 Comments

edited to include code examples and a link to where you can get the $.toJSON() plug-in.
nice one, i'm going to have a go at putting this in, it should simplify my code a fair bit. cheers!
Tried this, nothing showing up... I've got the 'jquery.json-2.2.js' include correctly linked in. I inserted a js alert to check contents of 'data' (its all in there) then when the posted page is returned and I do: $data=json_decode($_POST['data']); print_r($data); nothing shows, any ideas why?
Okay, so the JQuery JSON encode side is working. That's good. Have you checked the contents of $_POST before doing json_decode()? If that's blank then it's presumably the form that isn't posting correctly. I took the document.data.data.value and document.data.submit directly from your original code, so its possible they may not be correct. Try checking them in Firebug (or a similar browser debugging tool).
1

Not a direct answer- but if you need a quick fix you could do (in PHP):

// if $myarray=Array ( [0] => 9,8,6,5,4,3,2,15,1,0, ) ;

$myarray=explode(",",$myarray[0]);

Also, note that the split function is deprecated in PHP 5.3.0, you can replace with explode.

1 Comment

This did the trick for how I had it, but ended up using JSON as above instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.