แนวทางและโค้ดตัวอย่างต่อไปนี้ เป็นการประยุกต์ใช้งาน listbox หรือ
แท็ก select ที่มีความสัมพันธ์ของข้อมูล มากกว่า 2 รายการ ยกตัวอย่าง
เช่น การเลือกจังหวัด เลือกอำเภอ เลือกตำบล
หรือ การเลือกคณะ เลือกสาขา เลือกวิชา
หรือ เลือกยี่ห้อ เลือกประเภท และเลือก ขนาด / สี
แบบนี้เป็นต้น
เนื้อหานี้จะประยุกต์จากบทความเก่า เรื่อง
กำหนดรายการใน listbox ที่ 2 จากเงื่อนไขการเลือก listbox ที่ 1 ด้วย ajax ใน jquery อย่างง่าย
https://www.ninenik.com/content.php?arti_id=207 via @ninenik
ดังนั้นการใช้งานกับฐานข้อมูล สามารถดูจากบทความข้างต้น ได้
สำหรับในที่นี้จะใช้วิธีนำเสนอ เป็นแนวทาง สามารถนำไปประยุกต์หรือดัด
แปลงเพิ่มเติมได้ตามความเหมาะสม
ตัวอย่าง
ไฟล์ทดสอบ four_listbox.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | <!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >Document</ title > < style type = "text/css" > .mylabel{ display: inline-block; width: 150px; margin-bottom: 10px; } </ style > </ head > < body > < div style = "margin:auto;width:80%;" > < form id = "form1" name = "form1" method = "post" action = "" > < br > < br > < span class = "mylabel" >ตัวเลือกที่1: </ span > < select name = "list1" id = "list1" > < option value = "" >เลือกรายการ</ option > < option value = "1" >เลือกรายการ 1</ option > < option value = "2" >เลือกรายการ 2</ option > < option value = "3" >เลือกรายการ 3</ option > < option value = "4" >เลือกรายการ 4</ option > < option value = "5" >เลือกรายการ 5</ option > </ select > < br > < span class = "mylabel" >ตัวเลือกที่2: </ span > < select name = "list2" id = "list2" > < option value = "" >เลือกรายการ</ option > </ select > < br > < span class = "mylabel" >ตัวเลือกที่3: </ span > < select name = "list3" id = "list3" > < option value = "" >เลือกรายการ</ option > </ select > < br > < span class = "mylabel" >ตัวเลือกที่4: </ span > < select name = "list4" id = "list4" > < option value = "" >เลือกรายการ</ option > </ select > < br > < br > < span class = "mylabel" ></ span > < input type = "submit" name = "button" id = "button" value = "Submit" /> < br > </ form > </ div > < script src = "//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" ></ script > < script type = "text/javascript" > $(function(){ // เมื่อเปลี่ยนค่าของ select id เท่ากับ list1 $("select#list1").change(function(){ // ส่งค่า ตัวแปร list1 มีค่าเท่ากับค่าที่เลือก ส่งแบบ get ไปที่ไฟล์ data_for_list2.php $.get("data_for_list2.php",{ list1:$(this).val() },function(data){ // คืนค่ากลับมา $("select#list2").html(data); // นำค่าที่ได้ไปใส่ใน select id เท่ากับ list2 $("select#list2").trigger("change"); // อัพเดท list2 เพื่อให้ list2 ทำงานสำหรับรีเซ็ตค่า }); }); // เมื่อเปลี่ยนค่าของ select id เท่ากับ list2 $("select#list2").change(function(){ // ส่งค่า ตัวแปร list2 มีค่าเท่ากับค่าที่เลือก ส่งแบบ get ไปที่ไฟล์ data_for_list3.php $.get("data_for_list3.php",{ list2:$(this).val() },function(data){ // คืนค่ากลับมา $("select#list3").html(data); // นำค่าที่ได้ไปใส่ใน select id เท่ากับ list3 $("select#list3").trigger("change"); // อัพเดท list3 เพื่อให้ list3 ทำงานสำหรับรีเซ็ตค่า }); }); // เมื่อเปลี่ยนค่าของ select id เท่ากับ list3 $("select#list3").change(function(){ // ส่งค่า ตัวแปร list3 มีค่าเท่ากับค่าที่เลือก ส่งแบบ get ไปที่ไฟล์ data_for_list4.php $.get("data_for_list4.php",{ list3:$(this).val() },function(data){ // คืนค่ากลับมา $("select#list4").html(data); // นำค่าที่ได้ไปใส่ใน select id เท่ากับ list4 $("select#list4").trigger("change"); // อัพเดท list4 เพื่อให้ list4 ทำงานสำหรับรีเซ็ตค่า }); }); }); </ script > </ body > </ html > |
ไฟล์ data_for_list2.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php header( "Content-type:text/html; charset=UTF-8" ); header( "Cache-Control: no-store, no-cache, must-revalidate" ); header( "Cache-Control: post-check=0, pre-check=0" , false); ?> <?php if (isset( $_GET [ 'list1' ]) && $_GET [ 'list1' ]!= "" ){?> <option value= "" >เลือกรายการ</option> <?php for ( $i =1; $i <= $_GET [ 'list1' ]; $i ++){ ?> <option value= "<?=$i?>" >เลือกรายการ <?= $i ?></option> <?php } ?> <?php } else { ?> <option value= "" >เลือกรายการ</option> <?php } ?> |
ไฟล์ data_for_list3.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php header( "Content-type:text/html; charset=UTF-8" ); header( "Cache-Control: no-store, no-cache, must-revalidate" ); header( "Cache-Control: post-check=0, pre-check=0" , false); ?> <?php if (isset( $_GET [ 'list2' ]) && $_GET [ 'list2' ]!= "" ){?> <option value= "" >เลือกรายการ</option> <?php for ( $i =1; $i <= $_GET [ 'list2' ]; $i ++){ ?> <option value= "<?=$i?>" >เลือกรายการ <?= $i ?></option> <?php } ?> <?php } else { ?> <option value= "" >เลือกรายการ</option> <?php } ?> |
ไฟล์ data_for_list4.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php header( "Content-type:text/html; charset=UTF-8" ); header( "Cache-Control: no-store, no-cache, must-revalidate" ); header( "Cache-Control: post-check=0, pre-check=0" , false); ?> <?php if (isset( $_GET [ 'list3' ]) && $_GET [ 'list3' ]!= "" ){?> <option value= "" >เลือกรายการ</option> <?php for ( $i =1; $i <= $_GET [ 'list3' ]; $i ++){ ?> <option value= "<?=$i?>" >เลือกรายการ <?= $i ?></option> <?php } ?> <?php } else { ?> <option value= "" >เลือกรายการ</option> <?php } ?> |
สำหรับไฟล์ data_for_list(x).php นั้น ตามตัวอย่างจะทำแยกเพื่อให้เห็น
แนวทางชัดเจน ส่วนสำหรับ การไปใช้งานงาน อาจจะใข้เป็นไฟล์เดียวก็ได้
แล้วกำหนด if else เป็นเงื่อนไขสำหรับข้อมูลแทน