Html + js + PHP (using the smarty template technology) + mysql to implement a two-level dynamic drop-down list (select)

Source: Internet
Author: User
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);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.