html+js+php (using Smarty template technology) +mysql to implement a two-level dynamic drop-down list (select)

Source: Internet
Author: User
Tags string to json smarty template

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)

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.