供各種圖案窗簾、壁紙裝潢服務
專業到府丈量、送樣估價
估價網專業壁癌處理,效率施作、價格公道
人員現場估價,完全免費!

首頁  •  j2h 論壇 • 程式設計討論     • 

[JQuery] 搜尋單字

房東:小強
發表時間:2011-06-07


Lets filter a select box with jQuery, Sack, PHP and MySQL…..


You may or may not need this, but it could come in handy at one point in time. So lets start..


I will show you the basics here, you may download the source at the bottom of the page.


 


View the DEMO


index.html


include these into the head section of the page. ajax.js is the Sack js functions. I will get into Sack later.





1.<SCRIPT type=text/javascript src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"><!--mce:0--></SCRIPT> <SCRIPT type=text/javascript src="sack.js"><!--mce:1--></SCRIPT>



this goes into the head as well.





01.var ajax = new Array();

02.  

03.function getTextList(sel)

04.  

05.{

06.  

07.var color = document.getElementById('color').value; //gets value from text input

08.  

09.document.getElementById('colorReturn').options.length = 0;

10.  

11.if(color.length>0){

12.  

13.var index = ajax.length;

14.  

15.ajax[index] = new sack();

16.  

17.ajax[index].requestFile = 'getColor.php?color='+color; //sends the GET to the php page

18.  

19.ajax[index].onCompletion = function(){ createText(index) };

20.  

21.ajax[index].runAJAX();

22.  

23.}

24.  

25.}

26.  

27.function createText(index)

28.  

29.{

30.  

31.var obj = document.getElementById('colorReturn'); //retruns the options from the php page

32.  

33.eval(ajax[index].response);

34.  

35.}



The form





1.<FORM method=post sizcache="4" sizset="1">

2.<SELECT style="WIDTH: 200px" id=colorReturn size=5 name=colorReturn></SELECT>

3.<H4>Filter the colors - Start typing black.</H4>

4.<INPUT id=color onkeyup=getTextList(); name=color>

5.</FORM>



Thats it for the index page. pretty straight forward. So now for the fun part.


getColor.php – This page contains the sql and php that returns the





01.mysql_connect("localhost", "username", "password") or die(mysql_error());

02.  

03.mysql_select_db("database") or die(mysql_error());

04.  

05.$color = $_GET['color'];

06.  

07.if(isset($_GET['color'])){

08.  

09.$result = mysql_query("SELECT color FROM colors WHERE color LIKE '%$color%' LIMIT 0,10")

10.  

11.or die(mysql_error());

12.  

13.while($color = mysql_fetch_array( $result ))

14.  

15.{

16.  

17.echo 'obj.options[obj.options.length] = new Option("'.$color['color'].'","'.$color['color'].'");';

18.  

19.}

20.  

21.}



Its not the most detailed tutorial, but im not a tutorial guy.


View the DEMO


Download the SOURCE





  • 贊助網站       

    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!
    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務



  •  共 0 人回應

    姓名:
    佈告內容:
    其他選項: