Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > PHP > PHP Forum > listbox 2 ชั้นแบบ Ajax อยากให้กดปุ่ม แล้วดึงข้อมูลมาแสดงอะครับ


[PHP] listbox 2 ชั้นแบบ Ajax อยากให้กดปุ่ม แล้วดึงข้อมูลมาแสดงอะครับ

 
Topic : 043894



โพสกระทู้ ( 17 )
บทความ ( 0 )



สถานะออฟไลน์



Code (PHP)
01.<?php
02.     
03.    //ข้อมูลเกี่ยวกับฐานข้อมูลที่ใช้
04.   $DBServer = "localhost";
05.    $DBName = "My_Database";
06.    $DBUsername = "root";
07.    $DBPassword = "";
08.     
09.    $conn=mysql_connect( $DBServer, $DBUsername, $DBPassword ) or die( "ไม่สามารถติดต่อ
10.      กับ MySQLได้");
11.    mysql_select_db( $DBName ) or die( "ไม่สามารถเลือกใช้ฐานข้อมูล $dbname ได้" );
12. 
13.    @mysql_query("SET NAMES UTF8");
14.    $sql_select="select * from products where ProductType='$Tid' ";
15.    $result1= mysql_query( $sql_select, $conn );
16.    
17.     //เริ่มวนรอบแสดงข้อมูล
18.    while ($result = mysql_fetch_array($result1))
19. 
20.    //แสดงค่าในฐานข้อมูล
21.    {
22.          
23.    echo"$result[ProductName],";
24.     
25.   }
26. 
27.       // ปิดการเชื่อมต่อ
28.    mysql_close();
29.?>


html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ตัวอย่างการประยุกต์ใช้ AJAX กับ Double List Boxes</title>
<script type="text/javascript">
var xmlHttp;

function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function refreshproductList() {
var productType = document.getElementById("productType").value;
if(productType == "" ) {
clearproductList();
return;
}
var url = "DoubListBoxesData.php? Tid=" + productType;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}

function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
updateproductList();
}
}
}

function updateproductList() {
clearproductList();
var productName = document.getElementById("productName");
var results = xmlHttp.responseText;
var option = null;
p=results.split(",");
for (var i = 0; i < p.length; i++){
option = document.createElement("option");
option.appendChild(document.createTextNode(p[i]));
productName.appendChild(option);
}
}

function clearproductList() {
var productName = document.getElementById("productName");
while(productName.childNodes.length > 0) {
productName.removeChild(productName.childNodes[0]);
}
}
</script>
</head>
<body>
<p style="font-weight:bold;">ตัวอย่างการประยุกต์ใช้ AJAX กับ Double List Boxes</p>
<form action="#">
<span style="font-weight:bold;">ประเภทหนัง:</span>
<select id="productType" onchange="refreshproductList();">
<option value="0">กรุณาเลือกประเภทของหนัง</option>
<option value="1">หนังบู๊แอคชั่น</option>
<option value="2">หนังรักโรแมนติค</option>
<option value="3">หนังตลก</option>
<option value="4">หนังผี</option>
<option value="5">หนังลึกลับมหัศจรรย์</option>
<option value="6">หนังวิทยาศาสตร์</option>
</select>
<span style="font-weight:bold;">ชื่อหนัง:</span>
<select id="productName">
</select>

<!--อันนี้ ผมเขียนปุ่ม Search เพิ่มมาครับ ช่วยแนะนำทีครับเขียนตรงไหนเพิ่มถึงดึง
ข้อมูล ProductDetail ออกมาได้ครับ-->
<input type="submit" name="button" id="button" value="Search" />

</form>
</body>
</html>


ฐานข้อมูล

CREATE TABLE IF NOT EXISTS products (
ProductId int(11) NOT NULL auto_increment,
ProductName varchar(255) NOT NULL DEFAULT '' ,
ProductDetail text NOT NULL DEFAULT '' ,
ProductImage varchar(255) ,
ProductHot int(10) unsigned ,
ProductType int(10) unsigned ,
PRIMARY KEY (ProductId)
);


ผมอยากให้ คลิก ปุ่ม Search แล้วแสดงข้อมูลจาก field ProductDetail มาแสดงต้องทำไงครับ

ตัวอย่างนี้ผมเอา มาจากหนังสือ Web Programming ด้วย Ajax และ PHP:เคทีพี ครับ
ตัวอย่างนี้แค่ เลือกแปรผัน แบบ ajax ได้ เท่านั้น อ่ะครับ ช่วยแนะนำทีนะครับ



Tag : - - - -

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-06-11 09:45:21 By : Sacrifice View : 3907 Reply : 5
 

 

No. 1



โพสกระทู้ ( 1,751 )
บทความ ( 0 )



สถานะออฟไลน์


ลองเอาไปประยุกต์ดูนะครับ

Code (PHP)
001.     
002. 
003.<html>
004.<head>
005.<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
006.<title>Untitled Document</title>
007.</head>
008. 
009.<body>
010.<form name="form1" method="post" action="">
011.  <table width="496" border="0" cellspacing="0" cellpadding="5">
012.    <tr>
013.      <th scope="col"><select name="cusname" onChange="showtext()">
014.        <option selected>CUSTOMER</option>
015.        <option>C1</option>
016.        <option>C2</option>
017.        <option>C4</option>
018.        <option>C5</option>
019.        <option>C6</option>
020.        <option>C7</option>
021.        <option>C8</option>
022.        <option>C9</option>
023.        <option>C10</option>
024.        <option>C11</option>
025.        <option>C12</option>
026.        <option>C13</option>
027.        <option>C14</option>
028.        <option>C15</option>
029.        <option>C16</option>
030.        <option>C17</option>
031.        <option>C18</option>
032.        <option>C20</option>
033.      </select></th>
034.      <th scope="col"><select name="select1" onChange="changeSelect2(this)">
035.        <option  value="0" selected>กรุณาเลือกภาควิชา</option>
036.        <option  value="scin">คณะวิทยาศาสตร์</option>
037.        <option value="engi">คณะวิศวกรรมศาสตร์</option>
038.        <option value="arti">คณะศิลปศาสตร์</option>
039.        <option value="busi">คณะบริหารธุรกิจ</option>
040.        <option value="econo">คณะเศรษฐศาสตร์</option>
041.      </select></th>
042.    </tr>
043.    <tr>
044.      <td><input type="text" name="cuscountry" value = "COUNTRY" readonly="readonly"></td>
045.      <td><select name="select2" >
046.          <option selected>กรุณาเลือกภาควิชา</option>
047.      </select></td>
048.    </tr>
049.    <tr>
050.      <td><input type="text" name="cuszone" value="ZONE" readonly="readonly"></td>
051.      <td><select name="select3" >
052.          <option selected>กรุณาเลือกสาขา</option>
053.      </select></td>
054.    </tr>
055.  </table>
056.<br>
057.    <!-- ใส่ชื่ออัตโนมัติ-->
058.    <script language="javascript">
059. 
060.var shortcut=document.form1
061.var aut=new Array()
062.var aut1=new Array()
063. 
064. 
065.//extend this list if neccessary to accomodate more selections
066.aut[0]="COUNTRY"
067.aut[1]="PORTUGAL"
068.aut[2]="U.S.A."
069.aut[3]="ARGENTINA"
070.aut[4]="SPAIN"
071.aut[5]="GREECE"
072.aut[6]="SPAIN"
073.aut[7]="CANADA"
074.aut[8]="PERU"
075.aut[9]="PERU"
076.aut[10]="PERU"
077.aut[11]="PORTUGAL"
078.aut[12]="GREECE"
079.aut[13]="PORTUGAL"
080.aut[14]="POLAND"
081.aut[15]="ITALY"
082.aut[16]="THE  NETHERLANDS"
083.aut[17]="SPAIN"
084.aut[18]="MEXICO"
085.aut[19]="KOREA"
086.aut[20]="PERU"
087. 
088.aut1[0]="ZONE"
089.aut1[1]="EUROPE"
090.aut1[2]="AMERICA"
091.aut1[3]="AMERICA"
092.aut1[4]="EUROPE"
093.aut1[5]="EUROPE"
094.aut1[6]="EUROPE"
095.aut1[7]="AMERICA"
096.aut1[8]="AMERICA"
097.aut1[9]="AMERICA"
098.aut1[10]="AMERICA"
099.aut1[11]="EUROPE"
100.aut1[12]="EUROPE"
101.aut1[13]="EUROPE"
102.aut1[14]="EUROPE"
103.aut1[15]="EUROPE"
104.aut1[16]="EUROPE"
105.aut1[17]="EUROPE"
106.aut1[18]="AMERICA"
107.aut1[19]="ASIA"
108.aut1[20]="AMERICA"
109. 
110.function showtext(){
111.shortcut.cuscountry.value=aut[shortcut.cusname.selectedIndex]
112.shortcut.cuszone.value=aut1[shortcut.cusname.selectedIndex]
113.}
114.function changeSelect2(obj){
115.var theform = obj.form;
116.theform.select2.length=0;
117.theform.select3.length=0;
118.switch(obj.value){
119. case "0":
120. {
121.   theform.select2.options[0]=new Option('กรุณาเลือกภาควิชา','กรุณาเลือกภาควิชา');
122.   theform.select3.options[0]=new Option('กรุณาเลือกสาขา','กรุณาเลือกสาขา');
123.    
124.   theform.select2=false;
125. theform.select3=false;
126. }
127. break;
128.  
129. case"scin":
130. {
131.  theform.select2.options[0]=new Option('กรุณาเลือกภาควิชา','กรุณาเลือกภาควิชา');
132.  theform.select2.options[1]=new Option('วิทย์1','วิทย1');
133.  theform.select2.options[2]=new Option('วิทย์2','วิทย์2');
134.  theform.select2.options[3]=new Option('วิทย์3','วิทย3');
135.     theform.select2.options[4]=new Option('วิทย์4','วิทย์4');
136.  theform.select2.options[5]=new Option('วิทย์5','วิทย์5');
137. 
138.  theform.select3.options[0]=new Option('กรุณาเลือกสาขา','กรุณาเลือกสาขา');
139.  theform.select3.options[1]=new Option('วิทย์สาขา1','วิทย์สาขา1');
140.  theform.select3.options[2]=new Option('วิทย์สาขา2','วิทย์สาขา2');
141.  theform.select3.options[3]=new Option('วิทย์สาขา3','วิทย์สาขา3');
142.  theform.select3.options[4]=new Option('วิทย์สาขา4','วิทย์สาขา4');
143.  theform.select3.options[5]=new Option('วิทย์สาขา5','วิทย์สาขา5');
144.     
145.   theform.select2=false;  
146.   theform.select3=false;
147.  }
148.  break;
149.  
150.   
151. case"engi":
152. {
153.  theform.select2.options[0]=new Option('กรุณาเลือกภาควิชา','กรุณาเลือกภาควิชา');
154.  theform.select2.options[1]=new Option('วิศวะ1','วิศวะ1');
155.  theform.select2.options[2]=new Option('วิศวะ2','วิศวะ2');
156.  theform.select2.options[3]=new Option('วิศวะ3','วิศวะ3');
157.  theform.select2.options[4]=new Option('วิศวะ4','วิศวะ4');
158.  theform.select2.options[5]=new Option('วิศวะ5','วิศวะ5');
159.  theform.select2.options[6]=new Option('วิศวะ6','วิศวะ6');
160.  theform.select2.options[7]=new Option('วิศวะ7','วิศวะ7');
161.  theform.select2.options[8]=new Option('วิศวะ8','วิศวะ8');
162.  theform.select2.options[9]=new Option('วิศวะ9','วิศวะ9');
163.  theform.select2.options[10]=new Option('วิศวะ10','วิศวะ10');
164.  theform.select2.options[11]=new Option('วิศวะ11','วิศวะ11');
165.  theform.select2.options[12]=new Option('วิศวะ12','วิศวะ12');
166. 
167.  theform.select3.options[0]=new Option('กรุณาเลือกสาขา','กรุณาเลือกสาขา');
168.  theform.select3.options[1]=new Option('วิศวะสาขา1','วิศวะสาขา1');
169.  theform.select3.options[2]=new Option('วิศวะสาขา2','วิศวะสาขา2');
170.  theform.select3.options[3]=new Option('วิศวะสาขา3','วิศวะสาขา3');
171.  theform.select3.options[4]=new Option('วิศวะสาขา4','วิศวะสาขา4');
172.  theform.select3.options[5]=new Option('วิศวะสาขา5','วิศวะสาขา5');
173.   theform.select2=false;
174.   theform.select3=false; 
175.  }
176.  break;
177. 
178. 
179. case"arti":
180. {
181.  theform.select2.options[0]=new Option('กรุณาเลือกภาควิชา','กรุณาเลือกภาควิชา');
182.  theform.select2.options[1]=new Option('ศิลป1','ศิลป1');
183.  theform.select2.options[2]=new Option('ศิลป2','ศิลป2');
184.  theform.select2.options[3]=new Option('ศิลป3','ศิลป3');
185. 
186.  theform.select3.options[0]=new Option('กรุณาเลือกสาขา','กรุณาเลือกสาขา');
187.  theform.select3.options[1]=new Option('ศิลปสาขา1','ศิลปสาขา1');
188.  theform.select3.options[2]=new Option('ศิลปสาขา2','ศิลปสาขา2');
189.  theform.select3.options[3]=new Option('ศิลปสาขา3','ศิลปสาขา3');
190.  theform.select3.options[4]=new Option('ศิลปสาขา4','ศิลปสาขา4');
191.  theform.select3.options[5]=new Option('ศิลปสาขา5','ศิลปสาขา5');
192.   
193.   theform.select2=false;
194.   theform.select3=false;
195.  }
196.  break;
197. 
198. 
199. case"busi":
200. {
201.  theform.select2.options[0]=new Option('กรุณาเลือกภาควิชา','กรุณาเลือกภาควิชา');
202.  theform.select2.options[1]=new Option('BU1','BU1');
203.  theform.select2.options[2]=new Option('BU2','BU2');
204.  theform.select2.options[3]=new Option('BU3','BU3');
205.  theform.select2.options[4]=new Option('BU4','BU4');
206. 
207.  theform.select3.options[0]=new Option('กรุณาเลือกสาขา','กรุณาเลือกสาขา');
208.  theform.select3.options[1]=new Option('BUสาขา1','BUสาขา1');
209.  theform.select3.options[2]=new Option('BUสาขา2','BUสาขา2');
210.  theform.select3.options[3]=new Option('BUสาขา3','BUสาขา3');
211.  theform.select3.options[4]=new Option('BUสาขา4','BUสาขา4');
212.       
213.   theform.select2=false;
214.   theform.select3=false;  
215.    }
216.  break;
217. 
218. 
219. case"econo":
220. {
221.  theform.select2.options[0]=new Option('กรุณาเลือกภาควิชา','กรุณาเลือกภาควิชา');
222.  theform.select2.options[1]=new Option('EC1','EC1');
223.  theform.select2.options[2]=new Option('EC2','EC2');
224.  theform.select2.options[3]=new Option('EC3','EC3');
225.  theform.select2.options[4]=new Option('EC4','EC4');
226.  theform.select2.options[5]=new Option('EC5','EC5');
227.  theform.select2.options[6]=new Option('EC6','EC6');
228.  theform.select2.options[7]=new Option('EC7','EC7');
229. 
230.  theform.select3.options[0]=new Option('กรุณาเลือกสาขา','กรุณาเลือกสาขา');
231.  theform.select3.options[1]=new Option('ECสาขา1','ECสาขา1');
232.  theform.select3.options[2]=new Option('ECสาขา2','ECสาขา2');
233.  theform.select3.options[3]=new Option('ECสาขา3','ECสาขา3');
234.  theform.select3.options[4]=new Option('ECสาขา4','ECสาขา4');
235.  theform.select3.options[5]=new Option('ECสาขา5','ECสาขา5');
236.  theform.select3.options[6]=new Option('ECสาขา6','ECสาขา6');
237.  theform.select3.options[7]=new Option('ECสาขา7','ECสาขา7');
238.  theform.select3.options[8]=new Option('ECสาขา8','ECสาขา8');
239.  theform.select3.options[9]=new Option('ECสาขา9','ECสาขา9');
240.    
241.   theform.select2=false;
242.   theform.select3=false;  
243.  }
244.  break;   
245.  
246. }
247. }
248. 
249. 
250.    </script>
251.        </p>
252.</form>
253.</body>
254.</html>

Date : 2010-06-11 13:41:35 By : SOUL
 

 

No. 2



โพสกระทู้ ( 1,751 )
บทความ ( 0 )



สถานะออฟไลน์


ผมเห็นว่า Linkนี่น่ามีประโยชน์กับคุณนะครับ
https://www.thaicreate.com/community/dependant-listmenu-dropdownlist.html

ลองเข้าไปดูนะครับ
Date : 2010-06-11 14:02:06 By : SOUL
 

 

No. 3



โพสกระทู้ ( 17 )
บทความ ( 0 )



สถานะออฟไลน์


ข้างบนไม่ใช่ Ajax เป็น javaScript ธรรมดา นิครับ


พอดีผมอยากลองศึกษา Ajax ดูอ่ะครับ ใช้ java ก็ทำได้เหมือนกัน อย่าง #1 ถ้าข้อมูลเพิ่ม
ข้อมูล มันก็ต้องมาเขียนใหม่อะครับ อย่าง #2 ก็ไม่ใช่ Ajax นิครับ
ถ้าเป็น Ajax ต้องมีการใช้ XML ร่วมด้วย อย่าง

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");


ผมเขียน ให้กด button search แล้ว ข้อมูลออกมาแบบไม่กระพริบ ไม่เป็นอะครับ
อยากให้กกด ปุ่ม search แล้ว แสดงข้อมูลแบบไม่กระพริบเลยอ่ะครับ ช่วยแนะนำทีครับ
Date : 2010-06-11 14:18:16 By : Sacrifice
 

 

No. 4



โพสกระทู้ ( 48 )
บทความ ( 0 )



สถานะออฟไลน์


<script type="text/javascript">
function getRefresh() {
$("#mySpan").show("slow");
$("#mySpan").load("process_add.php?"+Math.random(), '', callback);
}

function callback() {
$("#mySpan").fadeIn("slow");
setTimeout("getRefresh();", 5000);
}
$(document).ready(
function(){
getRefresh();
}
);
</script>
<body>
<span id="mySpan"></span>

อันนี้ใช้ รีเฟรชครับ เผื่อเอาไปใช้ประโยชน์ได้ครับ
Date : 2011-06-24 10:51:30 By : aobbie_p
 

 

No. 5



โพสกระทู้ ( 34 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook

ถ้าเราต้องการค้นหาข้อมูลแบบมีเงื่อนไขมาก ๆเช่น and or not จะต้องมี code อะไรพิเศษนอกจาก %% ไหมค่ะ
Date : 2012-04-11 15:10:17 By : kankanit
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : listbox 2 ชั้นแบบ Ajax อยากให้กดปุ่ม แล้วดึงข้อมูลมาแสดงอะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่