duanlu2935 2015-03-22 17:18
浏览 26
已采纳

下拉选择问题

The following dropdown is filled with data from sql database:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
    <select name="Select" id="vals" onchange="this.form.submit()" class="btn btn-warning">
        <option value="">-- Select Country --</option>
        <?php 
           $user=new User(); 
           $sql5=DB::getInstance()->get('country', array('user', '=', $user->data()->username)); 
           if (!$sql5->count()) {
             echo 'No data'; 
           } else { 
             foreach ($sql5->results() as $sql5) { ?>
               <option value="<?php echo $sql5->name;?>">
               <?php echo $sql5->name;?></option>';
        <?php }
           } ?>
    </select>
</form>
</div>

And a user selects from the dropdown so that the information connecting to the selected data is outputted:

<? php
if (isset($_POST['Select']) && !empty($_POST['Select'])) {

    $userSelection = $_POST['Select'];
    $sql = DB::getInstance() - > get('country', array('country', '=', $userSelection));
    if (!$sql - > count()) {
        echo 'no data';
    } else {
        foreach($sql - > results() as $sql) {
            echo 'data';
        }

When I click an option from the dropdown, nothing comes out. The problem is with the $userSelection, a whitespace problem. When I replace the $userSelection with ' China', the data comes out but when I tried, 'China' nothing came out, it only works if there's a whitespace in front of it. I also checked my database and there's no whitespace in the values.

  • 写回答

1条回答 默认 最新

  • dongmu2174 2015-03-23 00:46
    关注
    foreach ($sql5->results() as $sql5) { ?>

    You are trying to iterate $sql5->results() and setting the child with the same variable name as the parent, thus replacing the parent. On the other iteration you are doing the same... try:

    <select name="Select" id="vals" onchange="this.form.submit()" class="btn btn-warning">
      <option value="">-- Select Country --</option>
      <?php 
      $user=new User(); 
      $sql5=DB::getInstance()->get('country', array('user', '=', $user->data()->username)); 
      if (!$sql5->count()) {
        echo '<option value="no-data">No data</option>'; 
      } else { 
        foreach ($sql5->results() as $item) { 
           echo '<option value="'.$item->name.'">'.$item->name.'</option>';
        }
     }
     ?>
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM