Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I can't seem to get this object to pass through jQuery's ajax to my PHP file.

$("body").on("submit", ".upload-results-form", function(){

        var photosJSON = [];

$(".upload-result-wrapper .upload-results .photos li").each(function(){

    var photoID = $(this).attr("rel");
    var description = $(".photo-upload-description", this).val();
    var source = $("img", this).attr("src");

    photosJSON.push({photoID: photoID, description: description, source: source});

});

var jsonData = JSON.stringify(photosJSON);


    $.ajax({
    type: "POST",
    url: "ajax/add/albums/photos_publish.php",
data: "photosJSON="+jsonData,
    cache: false,
    success: function(html){
        alert(html);        
    }
    });

}); 

my jsonData looks like this:

[{"photoID":"47","description":"","source":"photos/50611a8725cca_224.jpg"},
{"photoID":"48","description":"","source":"photos/50611a8764881_224.jpg"},
{"photoID":"49","description":"","source":"photos/50611a87aa508_224.jpg"},
{"photoID":"50","description":"","source":"photos/50611a88dd34b_224.jpg"}]

and my php file:

$photosJSON = json_decode($_POST['photosJSON']);
echo $photosJSON['photoID'];

however nothing is returned, it doesn't appear that anything is being sent through to php.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You must convert the javascript object into JSON:

data: {photoDescriptions: JSON.stringify(photoDescriptions)},

Then, in your PHP code:

$photoDescriptions = json_decode($_POST['photoDescriptions']);
share|improve this answer
    
Oops, I just realized I had the file directory off a tad bit, so not matter what I had it seemed like it wasn't working - because it wasn't! Thanks for your help as well –  Dylan Cross Sep 25 '12 at 4:44
    
And actually, it doesn't appear that anything is being sent to PHP. The string is empty –  Dylan Cross Sep 25 '12 at 4:55
    
Updated the code. Data being passed through http must be encoded, and jQuery does that automatically if pass data as object, otherwise you must use encodeURIComponent to encode the string. –  timidboy Sep 25 '12 at 5:40
    
That doesn't seem to work either :\ –  Dylan Cross Sep 25 '12 at 15:11

First things first: the "data" attribute of the AJAX call needs to be an object, like timidboy said.

Two quick sanity checks:

console.log(photosJSON);

right after the end of the .each(), and

var_dump($_POST);

somewhere in your PHP page. I suspect that, presuming you followed timidboy's corrections, the problem is not in your AJAX. Are you certain, for instance, that the identifier being iterated over by each() actually matches a DOM element on the page?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.