¦XªkÀu¨}·h®a¤½¥q
»ù®æ³z©ú¡A¦X¬ù«O»Ù¯u¦w¤ß
«È·½ºô©~®a²M¼ä½Ð§ä
©ö¸R²M¼ä¤½¥q

­º­¶  ¡E  tw ½×¾Â ¡E µ{¦¡³]­p°Q½×     ¡E 

[JQuery] cascade PHP+MYSQL°ÊºA¿ï³æÀ³¥Î

©ÐªF¡G¤p©f
µoªí®É¶¡¡G2011-03-23
[ÀËÁ|]


1.½Ð¥ý¤U¸üjqueryÀÉ®×

http://140.124.90.41/up/a/jquery.rar



2.¸ÑÀ£©ñ¸mºô¯¸¸ò¥Ø¿ý¸ê®Æ§¨¤U



PHP+MYSQL°ÊºA¿ï³æÀ³¥Î¡]by using Jquery¡^







¤@.»Ý¨D­¶­±¡Gtest.php and action.php¡Atest.php¬O¤U©Ô¿ï³æ³¡¥÷¦Óaction.php¬Ojquery¦b«á¥x³B²z¤U©Ô¿ï³æªº­¶­±





¤G.test.php



­º¨Ç½Ð©ó</head>¼ÐÅÒ«einclude¥H¤Uµ{¦¡½X,js§Ú¬O©ñ¦b¸ò¥Ø¿ýªºjquery¤U

<script type="text/javascript" src="jquery/jquery.js"></script>

<script type="text/javascript" src="jquery/jquery.cascade.js"></script>

<script type="text/javascript" src="jquery/jquery.cascade.ext.js"></script>

<script type="text/javascript" src="jquery/jquery.templating.js"></script>



­¶­±¤¤ªº¤U©Ô¿ï³æ¦³¤G-->class1¿ï³æÅã¥Ü¼t°ÓÃþ§O¦WºÙ¦Óc_name¿ï³æ¬O¨Ì¾Úclass1ªº­ÈÅã¥Ü¸ÓÃþ§O¤Uªº©Ò¦³¼t°Ó¦WºÙ



­ì©l½X¸ÑÄÀ¹Lµ{¦p¤U¡G



²Ä¤@¼hclass1¿ï³æµ{¦¡½X





½Æ»sµ{¦¡



<select name="class1[]" id="class1"> 
<option value="">Channel Type</option> 
<?php// ¸ê®Æ®w³]©w¶}©lmysql_select_db($database_mio, $mio); 
$query
= sprintf("SELECT DISTINCT class1 FROM company");//³o¦a¤è½Ð¤Usql»yªk¼´¥X²Ä¤@¼h¿ï³æ¸ê®Æ 
$result
= mysql_query($query, $mio);//¸ê®Æ®w³]©wµ²§ô 
 
//¼´¥X¸ê®Æ®w¼t°ÓÃþ§Oªº­È 
while ($row = mysql_fetch_assoc($result)) {    echo \'<option value="\' . $row["class1"] . \'">\' . $row["class1"] . \'</option>\' . "\\n";    }?></select>






²Ä¤G¼hc_name¿ï³æµ{¦¡½X

³o³¡¥÷¥u¬O¯Âºé°µ¤U©Ô¿ï³æ§Y¥i,¥Lªº­È·|§Q¥Îjquery¦^¶Ç²£¥Í



½Æ»sµ{¦¡



<select name="c_name[]" id="c_name"><option value="">Store Name</option></select>






ºò¸ò¦b²Ä¤G¼h¿ï³æ«á´¡¤Jjquery»yªk¨Ã°e¥X¦Üaction.php§@³B²z





½Æ»sµ{¦¡



<script> 
jQuery
(document).ready(function(){ $(\'#c_name\').cascade(\'#class1\', { 
//«e­±ªºc_nameªí¥Ü²Ä¤G¼h¿ï³æªºid,«á­±ªºclass1ªí¥Ü²Ä¤@¼h¿ï³æªºid   
ajax
: {      type: "GET",      url:  \'action.php\',//¶Ç°e¦Üaction.php     
              data
: { act: \'first\', val: $(\'#class1\').val() }//¶Ç°eÅܼÆact»Pvalµ¹action.php§@§PÂ_»P³B²z  },   
              template
: function(item) { return "<option value=\'" + item.Value + "\'>" +  item.Text + "</option>"; },   
              match
: function(selectedValue) { return this.When == selectedValue; }  });});</script>




¤T.action.phpµ{¦¡½X¸Ñ»¡





½Æ»sµ{¦¡



<?php require_once(\'Connections/mio.php\');//¤Þ¤J¸ê®Æ®w³sµ²ÀÉ ?> 
<?phpheader(\'Content-Type:text/html;charset=BIG5\');  
// ¸ê®Æ®w³]©wmysql_select_db($database_mio, $mio);if (!empty($_GET[\'act\']))  
{ $action = $_GET[\'act\'];}if (!empty($_GET[\'val\'])) 
   
{    $parentId = $_GET[\'val\'];}$list = \'[\';switch ($action) { 
//¦pªGact¡×first±¡ªp,´N±N¼t°ÓÃþ§O¬°$parentIdªº¼t°Ó¼´¥X¨Ó,¦Ó³o­Ó­È´N¬Otest.phpªº²Ä¤@¼h¤U©Ô¿ï³æªº­È,µM«á²Õ¦¨jqueryªºjson¸ê®Æ®æ¦¡µM«á¦^¶Ç//   
       
case \'first\':   $query = sprintf("SELECT C_name FROM company WHERE class1= \'$parentId\' ORDER BY C_name ASC");   $result = mysql_query($query, $mio);  
       
while ($row = mysql_fetch_assoc($result)) {   $list .= \'{\\\'When\\\':\\\'\' . $parentId . \'\\\',\\\'Value\\\':\\\'\' . $row["C_name"] . \'\\\',\\\'Text\\\':\\\'\' . $row["C_name"] . \'\\\'},\'; 
   
}    
   
break;    
   
}$list .= \']\'; 
echo $list
;?>






¥H¤W´N¬O²³æªº¤G¼h°ÊºA¿ï³æÀ³¥Î



­Y¬O­n¨Ï¥Î¤T¼hªº¸Ü,¤]¬O¦P¼Ëªº§@ªk¦b·s¼W¤@­ÓÄæ¦ì§@jquery



µ¹»Ý­nªº¤H§@°Ñ¦Ò.





  • ÃÙ§Uºô¯¸       

    ¼s§Q¤£°Ê²£-ªO¾ô¦b¦a¥Í®Ú³Ì¹ê¦b--·sªO¯S°Ï«ü¦W«×³Ì°ª¡B­È±o±z«H¿àªº¦n©Ð¥ò
    §¹¾ã©Ð°T¡A©Ð«Î¡B©±­±¼öªùºë¿ïª«¥ó¡A¼s§Q¤£°Ê²£ Àu½è¥ò¤¶¡A©Ð«Î¯²¸î¡B¶R½æ¸ê°T³z©ú¡A¥æ©ö¯u¦w¤ß¡I
    ¼s§Q¤£°Ê²£-·sªO¯S°Ï«ü¦W«×³Ì°ª¡B­È±o±z«H¿àªº¦n©Ð¥ò
    ±zªº¦«¥I,¼s§Q¥Î¤ß¬°±zªA°È

  • 1 ¼Ó¦í¤á¡Gjack
    µoªí®É¶¡¡G2011-03-23
    [ÀËÁ|]

    Ãö©óAjax¡A§Ú·Q³Ì´Îªº¦a¤è´N¬O¥i¥H©ó­I´º©I¥s¸ê®Æ®w¶Ç­È§a¡ã¦h­«¤U©Ô¦¡¿ï³æ´N¬O¤@¶µ«D±`´ÎªºAjaxÀ³¥Î¡F«e´X¤ÑJace¥á¹L¨Ó¤@­Ó°ê¥~ªººô§}¡GjQuery.cascade : Cascading values from forms¡A³o½g¤å³¹¥D­n¬O¦b»¡©újQueryªºcascade¡A¦Ó¥¦´N¬O¥Î¨Ó¹ê²{¦h­«¤U©Ô¦¡¿ï³æªº¥\¯à¡C

    ªá¤F¤@ÂI®É¶¡¹ê§@¤F¤@¤U¡Aµo²{«D±`²³æ´N¯à¹ê²{¡I¥H«e§Ú¤]§@¹LÃþ¦üªº¥\¯à¡A¥i¬Oªá¤F§Ú«D±`¦hªº®É¶¡...jQuery§â¥¦¥]¸Ë°_¨Ó¡AÅý¤@¤ÁÅܪºÂ²³æ¦h¤F¡F¥H¤U¬O²³æªº½d¨Ò¡Aµ¹¦³»Ý­nªº¤H°Ñ¦Ò§a¡G½d¨Ò¬O¤T¶¥¼hªºÃöÁp¦¡¦h­«¤U©Ô¦¡¿ï³æ¡A¤À¬°index.php¡]§e²{­¶¡^¡Baction.php¡]Ajax«áºÝ¸ê®Æ³B²z­¶¡^¡B¥H¤ÎjQueryªºcascade

    index.php¡G











    �Ĥ@�� �@
    �ĤG��
    �ĤT��





    action.php�G


    // ��Ʈw�]�w
    $host_sql = "localhost";
    $username_sql = "username";
    $password_sql = "password";

    $link = mysql_connect($host_sql, $username_sql, $password_sql) or die("�L�k�s����Ʈw");
    mysql_select_db('target', $link);
    mysql_query("SET NAMES UTF8");

    if (!empty($_GET['act'])) {
    $action = $_GET['act'];
    }

    if (!empty($_GET['val'])) {
    $parentId = $_GET['val'];
    }

    $list = '[';

    switch ($action) {
    case 'first':
    $query = "SELECT id, name FROM table where lv = 2 AND parentid= $parentId";
    $result = mysql_query($query, $link);
    while ($row = mysql_fetch_assoc($result)) {
    $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row["id"] . '\',\'Text\':\'' . $row["name"] . '\'},';
    }
    break;
    case 'second':
    default :
    $query = "SELECT id, name FROM table where lv = 3 AND parentid = $parentId";
    $result = mysql_query($query, $link);
    while ($row = mysql_fetch_assoc($result)) {
    $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row["id"] . '\',\'Text\':\'' . $row["name"] . '\'},';
    }
    break;
    }

    $list .= ']';
    echo $list;

    ��@�����I�O�b��ƪ��榡�G

    list = [{'When':'A1','Value':'W','Text':'SubchildA1a'},
    {'When':'A1','Value':'X','Text':'SubchildA1b'},
    ];

    �䤤When�N���W�@���h���ȡAValue�O���@���h���ȡATest�h�O�U�Ԧ������ܪ���r�F�n�S�O�`�NJavaScript�O�j�p�g�ӷP���I��@�o�ӽd�ҳ~���A���g�Q�j�p�g��F�@�q�ɶ�...��Ӥ~�o�{��ҥH�o�a��n�S�O�`�N�I�Ʊ�j�a���໴�P�g�X���p��Ʈw���h���U�Ԧ�����o^^http://blog.roodo.com/taikobo0/archives/6166625.html





    �@�@ 1 �H�^���@�@��ܭ��� �i��1 ���j 

    �m�W�G
    �G�i���e�G