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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?