2011-04-18 7 views
1

그래서 나는 폼에 mysql 테이블의 값으로 채워진 드롭 다운이 있습니다. 기본적으로이 양식은 선수를 팀에 추가하는 것입니다. 팀이 데이터베이스가 TEAM_ID과 쿼리 목록에서 선택하고 사람이 채울 수 있도록 팀의 모든 선수가 양식 아래 목록에 표시됩니다 때 내가하고 싶은 무엇Jquery로 을 기반으로 <select>

$seasonid = $_SESSION['SEASON_ID']; 
$sql="SELECT TEAM_ID, TEAM_NAME FROM TEAMS WHERE SEASON_ID=$seasonid"; 
$result=mysql_query($sql); 
$options=""; 
while ($row=mysql_fetch_array($result)) { 
     $tid=$row["TEAM_ID"]; 
     $tname=$row["TEAM_NAME"]; 
    $options.="<OPTION VALUE=\"$tid\">".$tname; 
} 

입니다 이 양식을 통해 누가 이미 팀에 속해 있는지 확인할 수 있습니다.

답변

2

HTML

<select id = 'teamSelect'>....options....</select> 
<div id = 'tableContainer'></div> 

자바 스크립트

$(function() { 
    $("#teamSelect").bind("change", function() { 
     $.ajax({ 
      type: "GET", 
      url: "path/to/server/script.php", 
      data: "tid="+$("#teamSelect").val(), 
      success: function(html) { 
       $("#tableContainer").html(html); 
      } 
     }); 
    }); 

}); 

서버 측 PHP 스크립트에 대한 아약스 요청을이 스크립트합니다 (코드에서이 path/to/server/script.php가) 수행 할 자바 스크립트 코드 데이터베이스를 쿼리하고 원하는대로 테이블을 출력하십시오. 선택된 팀은 $_GET 변수 'tid'가됩니다. 당신이해야 할 일은

+0

아름답게 일했습니다! 감사! – Rico

2

은 다음과 같습니다

1) attach a change handler to the dropdown using jQuery (http://api.jquery.com/change/) 

2) use $.get() (http://api.jquery.com/jQuery.get/) to make an async call to the server to query the database 

3) return the data (either as html directly or as a JSON See http://api.jquery.com/jQuery.getJSON/) 

4) in your $.get(), success handler either : 
     push the resultant html into a container element on the form (see http://api.jquery.com/html/), eg a <div> <ul> or <table> depending on what you returned from the server 

or parse the JSON and generate the html before rendering it into the form. 

당신은 당신이 용감 느낌이 경우 HTML 서식을 tmpl 플러그인을 사용할 수 있습니다. http://api.jquery.com/jquery.tmpl/

+0

입력에 감사드립니다 chrisp! – Rico

관련 문제