Hi,
I have a select menu of ‘Exam type’ which should call specific php arrays to create and populate other select menus. But I am getting odd results, i.e. selecting ‘AP’ for the exam type the ‘results’ select menu is populate with A,B.C,D,E,U and not 5,4,3,2,1 as it should.
Here is the HTML which includes the Javascript
<!DOCTYPE html>
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/func.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/cuislegibney/includes/arraybuilder.inc.php'; ?>
<html lang="en">
<head>
<link type="text/css" rel="stylesheet" href="/cuislegibney/css/stylesheet.css"/>
<meta charset="utf-8">
<title>Results Input</title>
</head>
<body>
<link rel="icon" type="image/x-icon" href="http://www.artgibney.com/artgibney/images/favicon.ico"/>
<header>
<h1>Results archive</h1>
</header>
<div class="input">
<table>
<tr>
<td>
<fieldset>
<legend>New student:</legend>
<table class="inner">
<tr>
<td>First name:</td><td><input type="textarea" name="action" value=""></td>
<td>Last name:</td><td><input type="textarea" name="action" value=""></td>
</tr>
<tr>
<td>Exam type:</td>
<td class="left">
<select name="exam" id="exam" style="background-color: #FFDDF4">
<option></option>
<?php foreach($exams as $key=>$option):
$selected = ($exam == $key) ? 'selected' : '';
echo "<option value='$key' $selected>$option</option>";
endforeach; ?>
</select>
</td>
<td>Subject:</td><td id="subject"><td>
</tr>
<tr>
<td>Year:</td><td id="year"></td>
<td>Result:</td><td id="result"></td>
</tr>
</table>
</fieldset>
</td>
</tr>
</table>
</div>
<script>
function examHandler() {
var subjectArray = [];
var a = true;
if(exam.value=="AS"){
subjectArray = <?php echo json_encode($ASsubjects, JSON_PRETTY_PRINT); ?>;
}else if(exam.value=="A2"){
subjectArray = <?php echo json_encode($A2subjects, JSON_PRETTY_PRINT); ?>;
}else if(exam.value=="AP"){
subjectArray = <?php echo json_encode($APsubjects, JSON_PRETTY_PRINT); ?>;
}else if(exam.value=="KET"||"PET"||"FCE"||"IELTS"){
document.getElementById('subject').innerHTML = "English";
a = false;
}
if(a){
var strOption = "";
subjectArray.forEach(function(item){
strOption += "<option value='test'>" + item + "</option>";
});
selectOption = "<select>" + strOption + "</select>";
document.getElementById('subject').innerHTML = selectOption;
}
};
function resultHandler() {
var resultArray = [];
if(exam.value=="AS"||"A2"){
resultArray = <?php echo json_encode($GCEresults, JSON_PRETTY_PRINT); ?>;
}else if(exam.value=="AP"){
resultArray = <?php echo json_encode($APresults, JSON_PRETTY_PRINT); ?>;
}else if(exam.value=="KET"||"PET"){
resultArray = <?php echo json_encode($KETPETresults, JSON_PRETTY_PRINT); ?>;/*KET and PET results are same*/
}else if(exam.value=="FCE"){
resultArray = <?php echo json_encode($FCEresults, JSON_PRETTY_PRINT); ?>;
}else if(exam.value=="IELTS"){
resultArray = <?php echo json_encode($IELTSresults, JSON_PRETTY_PRINT); ?>;
}
document.getElementById('result').innerHTML = "";/*clear the innerHTML*/
var strOption = "";
resultArray.forEach(function(item){
strOption += "<option value='result'>" + item + "</option>";
});
selectOption = "<select>" + strOption + "</select>";
/*console.log('results: ' + selectOption);*/
document.getElementById('result').innerHTML = selectOption;
};
function yearHandler() {
var yearArray = [];
yearArray = <?php echo json_encode($years, JSON_PRETTY_PRINT); ?>;
document.getElementById('year').innerHTML = "";
var strOption = "";
yearArray.forEach(function(item){
strOption += "<option value='year'>" + item + "</option>";
});
selectOption = "<select>" + strOption + "/<select>";
document.getElementById('year').innerHTML = selectOption;
};
function callFunctions(){
examHandler();
resultHandler();
yearHandler();
};
document.getElementById('exam').onchange = callFunctions;
</script>
</body>
</html>
And the array are in the file arraybuilder.inc.php
<?php
$exams = ['AS' => 'AS', 'A2' => 'A2', 'AP' => 'AP', 'KET' => 'KET', 'PET' => 'PET', 'FCE' => 'FCE', 'IELTS' => 'IELTS'];
$years = array("2015","2014","2013","2012","2011","2010","2009","2008");
$sessions = array("January","June");
$GCEresults = array("A","B","C","D","E","U");//Will need these as UMS scores
$ASsubjects = array("Mathematics","Geography","Physics","Biology","Chemistry");
$A2subjects = array("Pure Mathematics","Further Mathematics");
$APsubjects = array("Calculus","Chemistry","Physics","Biology");
$APresults = array("5","4","3","2","1");
$KETPETresults = array("Pass","Merit","Distinction");
$FCEresults = array("A","B","C","D");
$IELTSresults = array("1","2");
$SATsubjects = array("Mathematics","English","Reading");
$SATresults = array("200-800");
Any help would be greatly appreciated,
Thanks,
Shane