1

I am using jQuery and PHP to write JSON data to my server. I'm processing a decent amount of repeating numeric data (~.75kb), so I want to pass the data to PHP in the form of a multidimensional array.

At the moment I cannot manage to get the data to PHP in a form that it can recognize. I've tried various combinations of sending/receiving as arrays and objects with no success.

The best case scenario would be one in which I pass a the array to the PHP and the PHP converts it to a readable form. I'd rather not use associative arrays or any serializing on the part of the Javascript.

Code... This is giving me a 500 internal server error, which no longer occurs if I omit the passed data variable. (I'm not yet using $data in the php file yet because I know it's not working.)

function generateData() {
  // code here
  return [ one[ sub_one[], sub_two[] ], two[], three[], four[] /* Etc... */ ]
}
function saveData() {
    $.ajax({
        url: "scripts/save.php",
        data: {
            "area":"testing",
            "location":"testing",
            "name":"testing",
            "data":generateData()
        }
    });
}
<?php
    $area = $_GET['area'];
    $location = $_GET['location'];
    $name = $_GET['name'];
    $data = $_GET['data']);

    # Performing operations with variables...

    echo 1;
?>

Thanks for any help you can offer.

7
  • you got one extra bracket: $data = $_GET['data']); <-- Commented Mar 6, 2011 at 22:14
  • So I do... The 500 internal server error is gone, and I'm getting $data now. I'll try dumping $data and see if I can access any of it. Commented Mar 6, 2011 at 22:20
  • You should pass your data as JSON. Commented Mar 6, 2011 at 22:27
  • JSON can be accessed as an associative array in PHP, so why take an extra step to serialize JSON and pass it to the PHP to format it for my file when I can do this in a single step? Commented Mar 6, 2011 at 22:29
  • Short comment, you can't send objects over the web. Not from javascript to php, not from my pc to yours, there always needs to be some sort of serializing and deserializing involved, however, in your case jQuery and PHP takes care for that for you :). Commented Mar 6, 2011 at 22:30

1 Answer 1

1

Found a solution:

"data": {
    data: generateCellData()
}

The above code passes data as an object to PHP, whereby I can access the original array as $data("data"). I'm still somewhat baffled by why this works when I'm already passing the data and other parameters as an object.

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

Comments

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.