jquery ajax object array php

I have:

var apiQuizData = {'ect stuff removed...',answers:{}};
    $.each(dataCEActiveQuiz.quiz_data, function(index, answer) {
        if(answer.selected == undefined){
            apiQuizData.answers[answer.id] = 0;
        } else {
            apiQuizData.answers[answer.id] = answer.selected;

    $.post(URL, apiQuizData, function(data) {

If I look at the form data submitted through the header via chromes inspect tools it shows:

// url decoded

// url encoded

// query string


In PHP I use

$sent_data = file_get_contents('php://input');
$sent_data_decoded = json_decode($sent_data, true);

the string that php receives is


What do I need to do to the data so that it goes through to php with the values?




If I use

$.post(URL, JSON.stringify(apiQuizData), function(data) {

This is what is sent

{...extra stuff...,"answers":{"28195":"112773","28199":"112791","28201":"112796","28202":"112800","28204":"112810"}}

From PHP using json_decode(file_get_contents('php://input'), true);


When I do a print_r of the data it is an empty array?


UPDATE 2 - Working

Updated the jquery post to

    $.post(URL + 'sendCEQuizResults', {jsonstringify: JSON.stringify(apiQuizData)}, function(data) {

Updated the php receiving code to handle the new way I am sending data with the old way

$sent_data = file_get_contents('php://input');

            if(substr($sent_data, 0, 13) == 'jsonstringify')
                $sent_data_decoded = json_decode($jsonstringify, true);
            } else
                $sent_data_decoded = json_decode($sent_data, true);

For some reason it would not work if I didn't assign the JSON.stringify(apiQuizData) into the value of another object. The browser seemed to choke on the text by itself, I guess because it was a huge text string by itself? not sure. Either way the above update #2 solved the issues I was having.



Stringify the object into a JSON string:

$.post(URL, JSON.stringify(apiQuizData), function(data) {

Need Your Help

Window focus adds characters to shell

shell tmux fish

This is a very strange bug that I can't seem to track down. I'm using the fish shell and after I launch and disconnect from a TMUX session, every time the window loses focus a "[O" is added to the

Why does my input accept all values, even though I'm using a function to prevent this?

javascript html regex

I have this textbox on my website in which you can put a amount of euro's. So in this textbox it should only be possible to typ in numbers and comma's (no numbers).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.