Struggling To Work With JSON
Hey,
For a project of mine, a former Jquery/css designer created a neat app that takes JSON (created during page load)
and is then fed into a Jquery function that animates the results. Allow me to demonstrate:
A.) The JSON: (this is for a survey site, so the JSON is actually a list of survey options as well as the total # of responses each option has received as well as the % of responses they represent. Sort of like 'yes' having 44 responses and being 33% of all responses for that question)
-
var upny_iniresults = {
// "q_id" : "65977",
// "q_txt" : "What is your favorite genre of Music?",
"q_cdata" : [
{ "a_id" : "1", "a_txt" : "yes", "a_perc" : "50.00", "t_r" : "2" },
{ "a_id" : "2", "a_txt" : "no", "a_perc" : "25.00", "t_r" : "1" },
{ "a_id" : "3", "a_txt" : "not sure", "a_perc" : "25.00", "t_r" : "1" }
]
};
As you see, a var is created and we then pass that Json data to this function which, with the help of CSS I will not include,
loads up the survey results and displays them via a bar graph to show which options receive how many votes/etc.
-
function upny_liRes() {
var j = 0;
var a_html = new Array();
for(i = 0;i < upny_iniresults.q_cdata.length;i++) {
var t_html = '';
t_html += '<div class="upny_rlistttl">';
t_html += upny_iniresults.q_cdata[i].a_txt;
t_html += '<br><span id="tr_';
t_html += upny_iniresults.q_cdata[i].a_id;
t_html += '" class="upny_trtxt"></span>';
t_html += '</div><div class="upny_rlistcbr"><div style="position:relative;height:24px;"><div class="upny_rlistcbr1" style="width:1px;" id="a_';
t_html += upny_iniresults.q_cdata[i].a_id;
t_html += '"><div id="ap_';
t_html += upny_iniresults.q_cdata[i].a_id;
t_html += '">0';
t_html += '%</div></div><div class="upny_rlistcbr2"><div id="ap2_';
t_html += upny_iniresults.q_cdata[i].a_id;
t_html += '">0';
t_html += '%</div></div></div></div><div class="upny_clear"></div>\n';
a_html[j] = t_html;
j++;
}
$('#upny_resqanscntr').html(a_html.join('<div class="upny_dttdline"></div>'));
[b] upny_aniRes(upny_iniresults.q_cdata);[/b]
}
My Dilema:
As you see, I can successfully pass 'hard coded' json (json created during onload, via PHP)
and pass it to that function. For reasons I won't get into, it's NOT useful for the website
and the designer that left kept it that way.
My goal is to use external php pages, which can create JSON, and pass it to the function I listed.
The idea is I am passing the question_id of the survey, through a JQUERY variable (which I can do on my own)
and pass it to the external php page. In short, I am trying to do something like this:
var formData = $("#firstform").serialize(); // this works for us and includes the question_id, which is critical so we can pull proper JSON results of a recently answered survey.
.getJSON("www.mysite.com/external_json.php + formData");
And that would spit out the SAME json you see above and hand it to the upny_liRes function
How do I do that? How do I get the same 'load json in a bar graph' mode from external page json
as I do when it's hard coded in the example above?
Thanks