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

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动