Select menu population problems

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

What does the HTML look like when the page is loaded into a browser?

That should tell you whether the problem is with the JavaScript or with the PHP that is generating part of the JavaScript.

Hi,
Thanks for looking at this.
Before the ‘Exam type’ menu is used it looks like this.


After AP is selected from the ‘Exam type’ menu it looks like this,

The three new select menus are being generated. Both the ‘Subject’ and the ‘Year’ menus are correct, but the ‘Result’ menu is not correct, it should start with a 5 not an A.
Thanks,
Shane

You still haven’t shown us what the HTML looks like.

Hi,
Sorry, is it the page source I should post?

<!DOCTYPE html>
<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>
													 <option value='AS' >AS</option><option value='A2' >A2</option><option value='AP' >AP</option><option value='KET' >KET</option><option value='PET' >PET</option><option value='FCE' >FCE</option><option value='IELTS' >IELTS</option>												</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>
						<tr>
							 <td>
								 <fieldset>
								 <legend>Existing student:</legend>
									 <table class="inner">
										 <tr>
											 <td>First name:</td><td><input type="textarea" name="action" value=""></td>
											 <td>Exam type:</td><td><select><option value='test'>tester</option></select></td>
										 </tr>
										 <tr>
											 <td></td></td><td>
											 <td>Year:</td><td><input type="textarea" name="action" value=""></td>
										 </tr>
										 <tr>
											 <td></td></td><td>
											 <td>Result:</td><td><input type="textarea" name="action" value=""></td>
										 </tr>
									 </table>
								</fieldset>
							 </td>
						 </tr>
					 </table>
			  </div>
			  <script>
					function examHandler() {
						var subjectArray = [];
						var a = true;
						document.getElementById('subject').innerHTML = "";
						if(exam.value=="AS"){
							subjectArray = [
    "Mathematics",
    "Geography",
    "Physics",
    "Biology",
    "Chemistry"
];		
						}else if(exam.value=="A2"){
							subjectArray = [
    "Pure Mathematics",
    "Further Mathematics"
];
						}else if(exam.value=="AP"){
							subjectArray = [
    "Calculus",
    "Chemistry",
    "Physics",
    "Biology"
];
						}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 = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "U"
];		
						}else if(exam.value=="AP"){
							resultArray = [
    "5",
    "4",
    "3",
    "2",
    "1"
];
						}else if(exam.value=="KET"||"PET"){
							resultArray = [
    "Pass",
    "Merit",
    "Distinction"
];/*KET and PET results are same*/
						}else if(exam.value=="FCE"){
							resultArray = [
    "A",
    "B",
    "C",
    "D"
];
						}else if(exam.value=="IELTS"){
							resultArray = [
    "1",
    "2"
];
						}
							document.getElementById('result').innerHTML = "";
							var strOption = "";
							resultArray.forEach(function(item){	
								strOption += "<option value='result'>" + item + "</option>";
								});
							selectOption = "<select>" + strOption + "</select>";
							document.getElementById('result').innerHTML = selectOption;
						};
						
						function yearHandler() {
							var yearArray = [];
							yearArray = [
    "2015",
    "2014",
    "2013",
    "2012",
    "2011",
    "2010",
    "2009",
    "2008"
];		
							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>

Thanks,
Shane

should be

exam.value=="AS"|| exam.value=="A2"

Currently the test “A2” is always true and so the subsequent else clauses never execute.

1 Like

Thanks,
I made that correct, but still the result menu only offer the options A, B, C, D, U for every exam type. This should change.

<script>
                    function examHandler() {
                        var subjectArray = [];
                        var a = true;
                        document.getElementById('subject').innerHTML = "";
                        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"||exam.value=="PET"||exam.value=="FCE"||exam.value=="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"||exam.value=="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 = "";
                            var strOption = "";
                            resultArray.forEach(function(item){    
                                strOption += "<option value='result'>" + item + "</option>";
                                });
                            selectOption = "<select>" + strOption + "</select>";
                            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>

Thanks,
Shane

Got it,
Forgot to correct the first if() statement in resultHandler()

function resultHandler() {
                        var resultArray = [];
                        if(exam.value=="AS"|| exam.value=="A2"){
                            resultArray = <?php echo json_encode($GCEresults, JSON_PRETTY_PRINT); ?>;

Works great now thanks for your help,
Shane

Hi,
Is there any way to make the newly created select menus look the same as the exam type menu? I mean apart from just setting background colour to pink and moving them left.
Thanks,
Shane

PS I just fixed it. Set the background colour in the select tags and it works fine now,

selectOption = "<select style='background-color: #FFDDF4'>" + strOption + "/<select>";

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.