У меня есть веб-сайт, где я хочу использовать функцию javascript для обновления списка 2 и списка 3, когда выбран элемент из списка 1.
HTML
<select name="department_list" selected="All" style="min-width:80px" onchange="checkTeacherList(this.value, '<?php echo $user_login;?>');" > ... code to populate List 1 </select> </td> <td> <div id="departmentTeachers"></div> </td> <td> <div id="nondepartmentTeachers"></div> </td>
JAVASCRIPT
<script type="text/javascript"> function checkTeacherList(departmentName, schoolName) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("departmentTeachers").innerHTML = data["teacher1"]; document.getElementById("nondepartmentTeachers").innerHTML = data["teacher2"]; } } var d = new Date(); xmlhttp.open("GET","http://website/getTeacherInfo.php?schoolName="+schoolName+"&departmentName="+departmentName+"&nocache="+d.getSeconds(),true); alert ("test1"); // this alert displays var data = JSON.parse(xmlhttp.responseText); alert ("test2"); // this alert does not display xmlhttp.send(); } </script>
PHP
..... code that produces two strings $teacherList1 & $teacherList2 (which work correctly // return with a JSON type header('Content-Type: application/json'); // reply with json format echo json_encode(array('teacher1' => $teacherList1, 'teacher2' => $teacherList2));