html+js+PHP(使用了smarty模板技術)+mysql實現二級動態下拉式清單(select)

來源:互聯網
上載者:User

標籤:

目標

需要的是在選擇第一個select下拉式清單後,將選擇的內容傳送到PHP頁面。在PHP頁面中通過查詢數庫,得到第二個select需要顯示的option的值,並在select中顯示。

方案

1  利用ajax上傳資料到PHP,由於利用了smarty模板技術,可以直接給模板中的變數賦值,這樣就可以不用通過ajax接收返回值再經過處理輸出了。由於這個PHP頁面沒有包含在其他PHP頁面裡,是獨立的。這樣就造成無法賦值。

2  通過ajax接收返回值,這裡的返回值就是以上資料庫查詢的結果。    通過json_encode()將其轉化為json格式,然後輸出。    js中將responseText字串轉化為json格式——JSON.parse()函數實現。    遍曆數組,並添加option給select。
本次使用的是方案2

部分代碼
JS代碼:
function get_value(form){var scid=sub_class.value;//運算類型號//---------------------------------------//使用ajax發送選中的運算類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);//將字串轉化為數組for(var i=0; i<rst.length; i++){var opt=document.createElement('option');opt.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("你請求的分頁錯誤");}}}}

PHP代碼:
?phpheader ( "Content-type: text/html; charset=UTF-8" ); //設定檔案編碼格式require_once("system/system.inc.php");  //包含設定檔$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(使用了smarty模板技術)+mysql實現二級動態下拉式清單(select)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.