1

I'm creating a JSON arrary in Jquery and then sending it to php. In php I decode it but I get an error message. Below is an echo of the array that arrives in php and the error message. Note: I have only passed one item (to keep it smaller but in the further there would be more items in the array:

enter image description here

This is the current PHP code:

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

Also here is the jquery to generate the json array:

      jsonArray[thumbNum] = [{'src':val.attr('src')},
                            {'width':val.width()},
                            {'height':val.height()},
                            {'dataCX':val.attr('data-cx')},
                            {'dataCY':val.attr('data-cy')},
                            {'dataCW':val.attr('data-cw')},
                            {'dataCH':val.attr('data-ch')}
      ]
      thumbNum++;
    }

    $.post('scripts/php/join_processing.php', {
      'cropJoin': '1',
      'cropData': jsonArray},               
      function(data) {

Any advise on what I'm doing wrong here? I'm I sending an badly formed JSON array or not encoding it correctly?

thx

2
  • @donutdan4114: for what reason? Commented Sep 18, 2012 at 23:18
  • @zerkms: If you wanted to pass the JSON string through the POST. Commented Sep 18, 2012 at 23:20

4 Answers 4

0

Because the data-parameter of jQuery.post() is "intelligent guess", if you pass a JSON-string or a JS-object as data-parameter in jQuery.post(), it already posts it as an HTTP POST-array instead of a single json-string. So, what you're receiving in php is already a usable array, so there's no need to decode it.

Btw; you could've easily seen this since you print_r the POST-variable and it obviously prints it as an array instead of a string.

0

You don't need to decode it - it's already an array. So feel free to use it.

$.post passes it as an array, not as a json string.

0

Arrays in JavaScript are different than arrays in PHP. You're probably not meaning to send a JS array, but a JS object (which will turn into a PHP array).

Try this code instead...

jsonArray[thumbNum] = {
  src: val.attr('src'),
  width: val.width(),
  height: val.height(),
  // ..
};

While will translate into a php object roughtly like this:

array(
  array(
    "src" => "",
    "width" => "",
    "height" => ""
  ), array(
    "src" => "",
    "width" => "",
    "height" => ""
  )
);

The other posters are ALSO right that you don't really need to json_decode everything, because $.post will convert it properly for you.

0

You are not actually creating a JSON array there. JSON is a string notation that can represent an array. You are actually creating a regular JavaScript array. $.post will properly pass that to your PHP script so $_POST will be populated correctly. json_decode is not needed at all.

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.