Home >Backend Development >PHP Tutorial >Ajax asynchronous request for PHP data_PHP tutorial
I received an assignment from the teacher, and the implemented layout is as shown below:
If the department ID is entered, only the department name corresponding to the ID will be displayed. If not, all will be displayed. Then based on the value of the I department name, the text box in the corresponding major department will automatically display the major department where the department name is located. . For example: if Cardiovascular Medicine is selected, Internal Medicine will be displayed in the corresponding major department.
The main code is as follows:
Request department based on ID
function showHint(str) { var xmlhttp; if (window.XMLHttpRequest) {// IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// IE6, IE5 xmlhttp=new ActiveXObject(Microsoft.XMLHTTP); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById(txtHint).innerHTML=xmlhttp.responseText; } } xmlhttp.open(GET,keshi.php?q=+str,true); xmlhttp.send(); }
keshi.php:
<!--?php /*防止恶意调用*/ define(TEST,'test'); // 引入文件 include_once 'mysql.func.php'; // 数据库初始化 connectMySQL(); selectDB(); setZiFuJi(); //获得来自 URL 的 q 参数 $q=$_GET[q]; //如果 q 是数字或者数字字符串 if (is_numeric($q)) { $q = intval($q); $hint=; $resultDKQ = queryDB(select name from table_dake where id=$q); $hint = '科室名字:<select name=ksname id=ksname onchange=show(this.options[this.selectedIndex].value)-->'; while (!!$rowDKQ = fetchAssoc($resultDKQ)) { $hint .= '' ; } } // Not a number else { $resultDK = queryDB(select table_dake.name from table_dake); $hint = 'Department name: