Reputation: 424
I am trying to get data from a form submit. Here is the code.
function codeSubmission() {
$("#questionCodeForm").submit(function() {
$.post("SubmitCode.php", $("#questionCodeForm").serialize()).done(function(data) {
var questionName = data.questionName,
options = data.options,
pollingStatus = data.pollingStatus,
codeExist = data.codeExist;
alert(data);
alert(data[1])
alert(questionName);
alert(options);
if(codeExist == true) {
$("#quizTitle").text("questionName");
for(rowNum=1;rowNum<=5;rowNum++) {
$("#checkbox-"+rowNum).val("Answer1");
$("#checkbox"+rowNum+"label").text("Answer"+rowNum);
}
$("#answerForm").slideDown(500);
} else if(codeExist == false) {
alert("This quiz code is invalid");
}
});
return false;
});
return false;
}
Now the problem is that I cannot get the data into the variables I want. Furthermore I think the data is being sent as a string rather than an array. Here is the output of alert(data) for debugging purposes
{"questionName":"Test","pollingStatus":"0","options": {"1":"Test7","2":"Test8","3":"Test9","4":"Test10","5":"Test11"},"codeExist":true}
Now the above output from the jsonencode seems right. However to see the problem here is the output of data[0].
{
So I think the jsonencode is returning as a string. What I want to be able to do is access the data like questionName. How do I do this? Please help if you can.
Upvotes: 0
Views: 389
Reputation: 424
As OhGodWhy posted above, you also have to parse the json on the client side. You can do this using
obj = jQuery.parseJSON(data);
Upvotes: 0
Reputation: 4711
You can tell jQuery to use the "json" data-Type:
$.post('url', postData, function(returnData) {
alert(returnData.question);
}, 'json');
See documentation.
Upvotes: 1