dongzuan4917 2017-07-15 04:13
浏览 125
已采纳

如何从下拉列表中获取所选值并将其传递给sql查询

i have two dropdown list box,first one is sales area contain different kind of alphabet which get from cookie,second dropdown staff name is to change according to the selected value from first dropdown. How can i manage to pass the selected option value to my sql query so that it can be change according to the selected sales area. This is the results that i want to get I insert my code to the snippet for easy to do edit and demonstration.

function fetch_select(val)
{
 $.ajax({
 type: 'post',
 url: 'updateleave.php',
 data: {
  get_option:val
 },
 success: function (response) {
  document.getElementById("slct2").innerHTML=response; 
 }
 });
<table >
    <tr>
        
        <td>  Sales Area 
        <select name="Area" id="area" >
        <?php
   
        $sarea = explode(",",$_COOKIE['cooAreaCode']);
        foreach($sarea as $item){
        
        ?>
        <option   value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>
        
        <?php
        
        }
   


 
        ?>
       
       </select >
       </td>
       <?
           $var = $_POST['Area'];
      $sql = "SELECT  StaffName FROM tblStaff WHERE AreaCode= '$var'";
      $rs = odbc_exec($link,$sql);
     while ($row = odbc_fetch_array($rs)) {
         $porr[] = $row;
       }
       
      odbc_free_result($rs);   
     odbc_close($link); 
     ?>
        <td>  Staff Name 
        <select id="slct2">
    
        ?>
       
       </select>
       
       </td>
       <label class="form_field">Your selected <span id="aggregator_name"></span></label>

(updateleave.php)

if (isset($_POST['get_option'])) {

$item=$_POST['get_option'];

  $sql = "SELECT  StaffName FROM tblStaff WHERE AreaCode= '$item'";
  $rs = odbc_exec($link,$sql);
 while ($row = odbc_fetch_array($rs)) {
     $porr[] = $row;
   }
    for($i=0; $i < count($porr);$i++) {
   echo "<option  value="strtolower($porr[$i]['StaffName']);" >" .$porr[$i]['StaffName']."</option>";
  odbc_free_result($rs);   
 odbc_close($link); 
    }
?>
</div>
  • 写回答

2条回答 默认 最新

  • dql1978dql1978 2017-07-15 08:31
    关注

    Use append to add option tags with in select tag also do all the work in change event of the first drop down ("#area")

    $(document).ready(function(){
    $("#area").change(function()
    {
    var val =$(this).val();
     $.ajax({
     type: 'post',
     url: 'updateleave.php',
     data: {
      get_option:val
     },
     success: function (response) {
    $("#clct2").append(response);
    }
     });
    });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法