My application utilizes ajax in a specific section to send form variables to a php script, which then communicates with my api.
Below is the form:
<input name="answer_id" id="answer_id<?= $i;?>" type="radio" style="width: 21px" value="<?= $ans_id; ?>"> <!-- A for loop provides 3 different answers for the radio button here -->
<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'];
console.log("tempVar variable: " + tempVar);
console.log(tempVar);
}
The load_post() function:
function load_post(url, params){
// Code for loading post request goes here...
}
The variable responseData
stores a json array containing an object:
[{"id":"10082","question_id":"26","answer_id":"46","profile_id":"42121","correct":"1","attempts":"8"}]
When attempting to access this JSON object within the post_question() function, issues arise. Despite various attempts, such as storing it in a global variable or passing it to another function, the global variable remains empty. Yet, using tools like firebug reveals otherwise.
After seeking help from resources like Stack Overflow, I am still facing challenges in handling the responseData variable effectively. As a novice in javascript, any assistance on either passing or accessing this global variable would be greatly appreciated.