<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="jquery-1.11.2.min.js"></script> <title>無標題文檔</title> </head> <body> <select id="nation"></select> </body>
<script type="text/javascript"> $.ajax({ url:"jsonchuli.php", dataType:"JSON", success: function(data){ var str =""; /*for(var i=0;i<data.length;i++)//關聯數組里面的大小寫和數據庫里面的大小寫是要保持一致的。 { str = str+"<option value='"+data[i].Code+"'>"+data[i].Name+"</option>"; } $("#nation").html(str);*/ for(var s in data)//這種方法相當於foreach方法遍歷,上面注釋的方法是for循環。 { str = str+"<option value='"+data[s].Code+"'>"+data[s].Name+"</option>"; } $("#nation").html(str); } }) </script>
</html>
<?php include("DBDA.class.php"); $db = new DBDA(); $sql = "select * from nation"; //需要使用關聯數組 //數組內容的編碼格式必須是utf8的,其它的是不可以的。 echo json_encode($db->GuanQuery($sql));//返回的是關聯數組。json_encode返回的是json數據。