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.