Goal
What is needed is to transfer the selected content to the PHP page after selecting the first select drop-down list. In the PHP page, by querying the number library, you get the value of option that the second select needs to display and display in select.
Scheme
1 using AJAX to upload data to PHP, because of the use of Smarty template technology, you can directly assign values to variables in the template, so that you can not receive the return value through Ajax and processed output. Since this PHP page is not included in other PHP pages, it is independent. This makes it impossible to assign a value.
2 The return value is received via Ajax, where the return value is the result of the database query above. Convert it to JSON format by Json_encode () and output. JS converts responsetext string to JSON format--json.parse () function implementation. Iterate through the array and add option to select.
The use of this time is scenario 2
Part of the Code
JS Code:
function Get_value (form) {var scid=sub_class.value;//operation class Model//---------------------------------------// Use Ajax to send the selected operation class var url = "query_subject.php?scid=" +scid;xmlhttp.onreadystatechange = Content;//var url = "Query_ Subject.php? "; Xmlhttp.open ("GET", url,true); Xmlhttp.send (null);//---------------------------------------function Content () {if ( xmlhttp.readystate==4) {if (xmlhttp.status==200) {var Sel=document.getelementbyid ("Sub_name"); var rst=JSON.parse ( Xmlhttp.responsetext);//Convert the string to an array for (Var i=0; i<rst.length; i++) {var opt=document.createelement (' option '); o Pt.value=rst[i].sid;opt.text=rst[i].s_name;try { sel.add (opt,null);//Standards compliant } catch (ex) { sel.add (opt);//IE only }}}else{alert ("Page error you requested");}}}
PHP Code:
? Phpheader ("content-type:text/html; Charset=utf-8 "); Set the file encoding format require_once ("system/system.inc.php"); Include configuration file $scid=$_get[' SCID ']; $rst =null;if ($scid) {$sql = "Select Sid, S_name from subject where scid= ' $scid '"; $rst = $admindb->execsql ($sql, $conn);} Echo (Json_encode ($rst));? >
html+js+php (using Smarty template technology) +mysql to implement a two-level dynamic drop-down list (select)