4. Getting results in a JavaScript callback
Works in all modern browsers
To receive results in your JavaScript function:
- turn on “Enable API” in the survey settings;
- choose JS as the method;
- embed the survey on your page (get the code on the “Share” tab, “Embed” section);
- simpollCallback() function with one parameter must be defined on the same page.
The functionsimpollCallback() receives an object with the survey results.
Example of simpollCallback():
function simpollCallback(result){
alert('Poll ID: ' + result.sp_survey_id);
alert('Respondent name: ' + result.sp_resp.name);
if (result.sp_resp.quiz_passed == 1){
alert('Respondent has passed the test');
} else {
alert('Respondent has failed the test');
}
}
Example of the simpollCallback() argument:
{
"sp_survey_id": "551",
"sp_survey_key": "aa7ca07e",
"sp_resp": {
"id": "172712",
"ip_addr": "127.0.0.1",
"name": "Respondent #172712",
"time_start": "2011-12-13 16:17:00",
"time_end": "2011-12-13 16:17:13",
"time_delta": "00:00:13",
"quiz_passed": "0",
"quiz_percent": "0",
"quiz_correct": "1",
"quiz_wrong": "3"
},
"sp_result": "1",
"sp_answers": {
"1": ["Spain"],
"2": ["Pyramids", "Kremlin", "Colosseum"]
}
}
Names, structure and values of the fields are the same as described in 3.2.
If several surveys are embedded on one page, the simpollCallback() function is still defined only once. You can tell which survey sent the results by the sp_survey_id field of the object.