2012-11-12 87 views
1

我已經搜索了高分辨率和低分辨率的此總線尚未能夠解決。當我想要一個動態下拉菜單來調整第二個下拉菜單中的值並在文本框中填入文本值時,我設法讓它起作用。動態下拉列表以填寫文本框

現在我想切出第二步:即。我其實想擺脫第二個下拉菜單,只需在文本框中輸入一個值即可。我已經嘗試了一切,以消除第二步,但一旦我做了一切停止工作。

目前功能着眼於第二個下拉並設置選項,並增加我行

的document.getElementById(「fld_ClientID」)。價值=「」

得到它在文本框中輸入數據。我如何完全擺脫對tblPromotions的引用,並使其僅獲取文本框的數據。

<script language="javascript"> 
function setOptions(chosen) { 
    var selbox = document.myform.selectpromotion; 

    selbox.options.length = 0; 
    if (chosen == "0") { 
    selbox.options[selbox.options.length] = new Option('First select a client','0'); 

    } 
    <?php 
    $client_result = mysql_query("SELECT * FROM tblClients ORDER BY ClientName") or die(mysql_error()); 
    while(@($c=mysql_fetch_array($client_result))) 
    { 
    ?> 
    if (chosen == "<?=$c['ClientID'];?>") { 

    <?php 
    $c_id = $c['ClientID']; 
    $promo_result = mysql_query("SELECT * FROM tblPromotions WHERE ClientID='$c_id'") or die(mysql_error()); 
    while(@($m=mysql_fetch_array($promo_result))) 
    { 
    ?> 
     selbox.options[selbox.options.length] = new 
     Option('<?=$m['PromotionName'];?>','<?=$m['ClientID'];?>'); 
     document.getElementById('fld_ClientID').value ="<?=$m['ClientID'];?>"; 
    <?php 
    } 
    ?> 
    } 
    <?php 
    } 
    ?> 
} 
</script> 
</head> 
<body> 
<form name="myform" method="POST" action="processaddpromotionNEW.php"> ><div align="center"> 
    <p> 
    <select name="selectclient" size="1" 
    onchange="setOptions(document.myform.selectclient.options 
    [document.myform.selectclient.selectedIndex].value);"> 
    <option value="0" selected>Select a client</option> 
    <?php 
    $result = mysql_query("SELECT * FROM tblClients ORDER BY ClientName") or die(mysql_error()); 
     while(@($r=mysql_fetch_array($result))) 
    { 
    ?> 
    <option value="<?=$r['ClientID'];?>"> 
     <?=$r['ClientName'];?> 
     </option> 
    <?php 
    } 
    ?> 
    </select> 
    <br><br> 
    <select name="selectpromotion" size="1"> 
    <option value=" " selected>First select a client</option> 
    </select> 
    </p> 
    <p> 
<input name="fld_ClientID" type="text" class="Arial" id="fld_ClientID" tabindex="11" size="10" /> 
    <br> 

回答

0

也許,你不應該在一個javascript函數中做一個mysql查詢。你應該: 使用AJAX來獲取可能的選項 或 查詢所有可能的選項一次,然後將它保存在一個全局變量

類似:

<script> 
var secondOptions = {}; 
<?php 
    $promo_result = mysql_query("SELECT * FROM tblPromotions") or die(mysql_error()); 
    while(@($m=mysql_fetch_array($promo_result))){ 
    ?> 
     if(secondOptions['<?=$m['ClientID'];?>'] == undefined) 
      secondOptions['<?=$m['ClientID'];?>'] = {}; 
     secondOptions['<?=$m['ClientID'];?>']['<?=$m['PromotionId'];?>'] = '<?=$m['PromotionName'];?>'; 
    <?php 
    } 
    ?> 

    setOptions(clientId){ 
     jQuery("select[name=selectpromotion]").empty(); 
     options = ""; 
     jQuery.each(secondOptions[clientID],function(a,b){ 
      options += "<option value='"+a+"'>"+b+"</options>"; 
      }); 
     jQuery("select[name=selectpromotion]").append(options); 
    } 

    <select name="selectclient" size="1" onchange="setOptions(jQuery(this).val());"> 

    ...