douyou2048 2016-03-13 15:36
浏览 65
已采纳

如何从数组中删除空字符串。 我不确定天气是来自PHP还是Ajax

I am using Ajax in order to display drop-down list when a product group is selected. My Ajax returns a list from the query page(dropd.php). However the problem is the array consists of some empty space between each element. Hence when I did inspect in chrome below is the output I am getting,

<option value="1">Gear pumps</option>
<option></option>
<option value="2">Piston pumps</option>
<option></option> 

How could I remove "<option></option>" from the drop-down list? If I take option tag out from html what I receive is;

Gear pumps^piston pumps^vane pumps...etc If you can notice the space(^).

Ajax code

<script>
    $(document).ready(function(){
      $('#sel1').on('change', function(){
          $.post("dropd.php",{vals:$("#sel1 option:selected").text()},
          function(data){$.trim($("#sel2").html( data ));});
         });
    });
</script>

dropd.php

<?php 
  require('../config/connection.php'); 
  if($_POST['vals']){   
  $values = mysqli_real_escape_string($dbc,$_POST['vals']);
  $query = "SELECT * FROM prdct_categories WHERE product = '$values'";
  $result = mysqli_query($dbc, $query);
  ?>
    <option value="">Select subgroup</option>
    <?php
    foreach($result as $row)
    { ?>
  <option value="<?php echo $row['id']; ?>"><?php echo $row['subgroup']; ?><option/>        
       <?php
        }
     }
    mysqli_close($dbc);
  ?>

Ajax data goes here

  <div class="form-group">
    <label for="inputsubprdctgrp" class="col-sm-4 control-label" >Product Subgroup</label>
    <div class="col-sm-8">
        <select class="form-control" id="sel2" >

        </select>
    </div>
  </div>

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • duancutan4770 2016-03-13 15:50
    关注

    There may be 2 issues:

    1. The SQL result gives an empty value which can be fixed by using the array_filter() method:

      $new_result = array_filter($result);

    2. You wrote option/ instead of **/option

    Hope this helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥65 LineageOs-21.0系统编译问题
  • ¥30 关于#c++#的问题,请各位专家解答!
  • ¥15 App的会员连续扣费
  • ¥15 不同数据类型的特征融合应该怎么做
  • ¥15 用proteus软件设计一个基于8086微处理器的简易温度计
  • ¥15 用联想小新14Pro
  • ¥15 multisim中关于74ls192n和DSWPK开关仿真图分析(减法计数器)
  • ¥15 w3wp,exe 中发生未处理的 Microsoft ,NETFramework 异常。
  • ¥20 C51单片机程序及仿真(加减器)
  • ¥15 AQWA | 水动力分析 二阶波浪力