动态下拉列表以填写文本框

时间:2012-11-12 07:52:06

标签: php javascript mysql drop-down-menu onchange

我已经搜索了高低分辨率,这辆公共汽车还没能解决。当我想要动态下拉以在第二个下拉列表中调整值并在文本框中填写文本值时,我设法让这个工作。

现在我想切出第二步:即。我实际上想摆脱第二次下拉,只需在文本框中输入一个值。我已经尝试了一切来删除第二步,但一旦我做了一切就停止工作。

此时,该功能会查看第二个下拉菜单并为其设置选项,然后我添加了一行

document.getElementById('fld_ClientID')。value =“”

让它在文本框中输入数据。如何完全摆脱对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> 

1 个答案:

答案 0 :(得分: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());">  

    ...