I use ajax in a small part of my application to post form variables to a php script that makes a query to my api.
Here is the form
<input name="answer_id" id="answer_id<?= $i;?>" type="radio" style="width: 21px" value="<?= $ans_id; ?>"> <!-- I have a for loop here that provides 3 different answers for the radio button -->
<input name="question_id" id="question_id" type="hidden" style="width: 21px" value="<?= $question_id; ?>">
<input name="all_answers" id="all_answers" type="hidden" style="width: 21px" value='<?= serialize($answers); ?>'>
<input name="Submit" onclick="post_question()" type="submit" value="Submit" class="btn btn-success">
<input type="hidden" id="campaign_id" value="<?= $campaign; ?>">
The post_question() function
var responseData = [];
function post_question(){
var data = new FormData(),
obj,
campaign = document.getElementById('campaign_id').value;
data.append("all_answers", document.getElementById('all_answers').value);
data.append("question_id", document.getElementById('question_id').value);
data.append("campaign_id", document.getElementById('campaign_id').value);
load_post("controllers/post_question.php", data);
var tempVar = responseData['response'];//The problem begins here. responseData['response'] appears to be an empty string
console.log("tempVar variable: " + tempVar);
console.log(tempVar);
}
The load_post() function
function load_post(url, params){
new imageLoader(cImageSrc, 'startAnimation()');
var xhr,
response = [];
if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest();
else {
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"]
for(var i = 0, len = versions.length; i < len; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
}
catch(e){}
} // end for
}
xhr.onreadystatechange = ensureReadiness;
function ensureReadiness() {
if (xhr.readyState !== 4) {
return;
}
responseData['response'] = xhr.responseText;
responseData['code'] = xhr.status;
}
xhr.open('POST', url, true);
xhr.send(params);
responseData['response'] = xhr.responseText;
responseData['code'] = xhr.status;
return responseData;
}
The variable responseData
is a global variable. It is an array. responseData['response']
is a json array containing an object.
[{"id":"10082","question_id":"26","answer_id":"46","profile_id":"42121","correct":"1","attempts":"8"}]
and that is what is returned from the post_question.php page. What I have trouble doing is accessing that JSON object, I've tried storing it in a global variable and I've tried passing it to another function with no success. And for some reason the global variable says its an empty string. however, using firebug checking the responseData variable, this is what I get:
This didn't quite solve my problem and neither did this there's a few other resources I've gone to that didn't quite help solve my problem. What I would like is to either pass the responseData variable that gets saved in load_post() or access that variable from the post_question() function, it is a global variable after all. Any help would be appreciated. P.S. I'm new to javascript.