인터넷정보

동적인 다중셀렉트 샘플

알 수 없는 사용자 2007. 10. 13. 16:21

단순한 에제입니다.

########## main.php ##########
<?
if($_GET["A"]){
    $Query ="select fieldB from 테이블 where fieldA='".$_GET["A"]."'";
    $cnn = mysql_query($Query) or exit(mysql_error());
    while($rst = mysql_fetch_assoc($cnn)) {
       $RecordsB = array_merge($RecordsB, array($rst));
    }
}

if($_GET["B"]){
    $Query ="select fieldC from 테이블 where fieldA='".$_GET["A"]."' and fieldB='".$_GET["B"]."'";
    $cnn = mysql_query($Query) or exit(mysql_error());
    while($rst = mysql_fetch_assoc($cnn)) {
       $RecordsC = array_merge($RecordsC, array($rst));
    }
}
?>

<script language="JavaScript" id="DynamicA"></script>
<script language="JavaScript" id="DynamicB"></script>
<script language="JavaScript">
<!--
function makeB(){
    var ValueA = document.getElementsByName('A')[0].value;
    DynamicA.src = "makeB.php?selectA="+ValueA;
}

function makeC(){
    var ValueA = document.getElementsByName('A')[0].value;
    var ValueB = document.getElementsByName('B')[0].value;
    DynamicB.src = "makeC.php?selectA="+ValueA+"&selectB="+ValueB;
}

function checkForm(){
    var ValueA = document.getElementsByName('A')[0].value;
    var ValueB = document.getElementsByName('B')[0].value;
    document.forms["F"].action = "main.php?A="+ValueA+"&B="+ValueB;
    document.forms["F"].submit();
}
//-->
</script>

<form name="F" method="post">
<select name="A" onChange="makeB()">
<option value="">--선택--</option>
<option value="현대" <?if($RecordsB[$i]["fieldA"]==$_GET["A"]) echo"selected";?>>현대</option>
<option value="대우" <?if($RecordsB[$i]["fieldA"]==$_GET["A"]) echo"selected";?>>대우</option>
<option value="삼성" <?if($RecordsB[$i]["fieldA"]==$_GET["A"]) echo"selected";?>>삼성</option>
</select>

<select name="B" onChange="makeC()">
<option value="">--선택--</option>
<?for($i=0;$i<count($RecordsB);$i++){?>
    <option value="<?=$RecordsB[$i]["fieldB"]?>" <?if($RecordsB[$i]["fieldB"]==$_GET["B"]) echo"selected";?>><?=$RecordsB[$i]["fieldB"]?></option>
<?}?>
</select>

<select name="C">
<option value="">--선택--</option>
<?for($i=0;$i<count($RecordsC);$i++){?>
    <option value="<?=$RecordsC[$i]["fieldC"]?>" <?if($RecordsC[$i]["fieldC"]==$_GET["C"]) echo"selected";?>><?=$RecordsC[$i]["fieldC"]?></option>
<?}?>
</select>

<input type="button" value="확인" onClick="checkForm()">
</form>
########## //main.php ##########

########## makeB.php ##########
<?
$Query ="select fieldB from 테이블 where fieldA='".$_GET["selectA"]."'";
$cnn = mysql_query($Query) or exit(mysql_error());
while($rst = mysql_fetch_assoc($cnn)) {
    $RecordsB = array_merge($RecordsB, array($rst));
}
?>

var ObjB = document.getElementsByName('B')[0].options;
for(var i=ObjB.length;i>=0;i--) ObjB[i] = null;
ObjB[0] = new Option('--선택--','');
<?for($i=0;$i<count($RecordsB);$i++){?>
    ObjB[ObjB.length] = new Option('<?=$RecordsB[$i]["fieldB"]?>','<?=$RecordsB[$i]["fieldB"]?>');
<?}?>
########## //makeB.php ##########

########## makeC.php ##########
<?
$Query ="select fieldC from 테이블 where fieldA='".$_GET["selectA"]."' and fieldB='".$_GET["selectB"]."'";
$cnn = mysql_query($Query) or exit(mysql_error());
while($rst = mysql_fetch_assoc($cnn)) {
    $RecordsC = array_merge($RecordsC, array($rst));
}
?>

var ObjC = document.getElementsByName('C')[0].options;
for(var i=ObjC.length;i>=0;i--) ObjC[i] = null;
ObjC[0] = new Option('--선택--','');
<?for($i=0;$i<count($RecordsC);$i++){?>
    ObjC[ObjC.length] = new Option('<?=$RecordsC[$i]["fieldC"]?>','<?=$RecordsC[$i]["fieldC"]?>');
<?}?>
########## //makeC.php ##########

출처 : 지우넷

반응형