Html + js + PHP (using the smarty template technology) + mysql achieve the secondary dynamic drop-down list (select) target
You need to transfer the selected content to the PHP page after selecting the first select drop-down list. On the PHP page, query the database to obtain the option value to be displayed for the second select statement and display it in the select statement.
Solution
1. Upload data to PHP using ajax. because of the smarty template technology, you can directly assign values to the variables in the template, so that you do not need to receive the returned values through ajax and then process the output.
This PHP page is independent of other PHP pages. In this way, the value cannot be assigned.
2. receive the return value through ajax. The return value here is the result of the above database query.
Use json_encode () to convert it to json format and then output it.
In js, convert the responseText string to json format ?? JSON. parse () function implementation.
Traverse the array and add option to select.
Solution 2 is used this time.
Some code
JS code:
Function get_value (form) {var scid = sub_class.value; // operation type number // ----------------------------------------- // 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); // converts a string to an array for (var I = 0; I
PHP code:
? Phpheader ("Content-type: text/html; charset = UTF-8"); // sets the file encoding format require_once ("system/system. inc. php "); // contains the 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);?>