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
answers[28194]:112768
answers[28195]:112773
answers[28199]:112788
answers[28202]:112803
answers[28204]:112809

// url encoded
answers%5B28194%5D:112768
answers%5B28195%5D:112773
answers%5B28199%5D:112788
answers%5B28202%5D:112803
answers%5B28204%5D:112809

// query string

answers%5B28195%5D=112773&answers%5B28199%5D=112788&answers%5B28202%5D=112803&answers%5B28204%5D=112809

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

&answers=&answers=&answers=&answers=&answers=

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

Thanks.

=================

UPDATE 1

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);

{...extrastuff...}id%22%3A952077%2C%22answers%22%3A%7B%2228195%22%3A%22112

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')
            {
                parse_str($sent_data);
                $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.

Thanks.

Answers


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.