【问题标题】:How to set the default value in a dynamic drop down如何在动态下拉列表中设置默认值
【发布时间】:2014-10-20 04:47:43
【问题描述】:

我有一个从MySQL database 动态填充的drop down select,我需要根据用户选择的记录默认显示下拉值。到目前为止,我没有在下拉列表中显示任何值。但是我有选择选项语句之外的代码,它以粗体显示默认值。我似乎无法通过下拉菜单来遍历 if 语句逻辑。

用户从数据表中选择一条记录并单击编辑。例如,用户选择了以灰色突出显示的行,然后单击编辑将其发送到编辑表单

编辑表单:我将单元 id 传递给编辑表单并根据传递的 id 预填充输入字段,但这里我无法填充和设置默认下拉选项值

这是下拉选择(div id 下拉)的编辑表单背后的代码

    define('DBHOST','localhost');
    define('DBUSER','root');
    define('DBPASS','*********');
    define('DBNAME','fdmamaint');

    if (!$db = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME)) die("Can't connect to database");

 // Retrieve the record the user selected
    $id = htmlspecialchars($_GET["id"]); 

// Query the depunits table based on the user selection 
    $sql = "SELECT unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind "
            . "FROM depunits "
            . "WHERE unit_id=$id";
    if (!$result = $db->query($sql)){
            die("There was an error running the query [" .$db->error. "]");
    }

// Walk through the result set and assign variables for reference later on in the script
    while ($row = $result->fetch_assoc()){
        $unitId = $row['unit_id'];
        $divId = $row['div_id'];
        $titleOrg = $row['title_org'];
        $shortDesc = $row['short_desc'];
        $unitDesc = $row['unit_desc'];
        $longDesc = $row['long_desc'];
        $enabled = $row['avail_ind'];
    }

  // Div Id - query database to get drop down select options
    $divSelect = "SELECT div_id, long_desc FROM depdivisions "
            . " WHERE div_id <> '' and avail_ind='Y' and active_ind='Y'"
            . " ORDER BY div_id";
    if (!$divResult = $db->query($divSelect)){
            die("There was an error running the query [" .$db->error. "]");
    }

.......

动态下拉选择:

<!-- Div Id -->                                        
<label for="divId" class="control-label">Div Id</label>
   <select class="form-control" name="divId" id="divId">
       <option value=" "></option>
       <?php
          while ($rowDivs = $divResult->fetch_assoc()) 
         {
            $data = '37' .$rowDivs['div_id'];

            // if the value of $data matches the value of div id of the 
            // user selected row in the database then give that option 
            // value the selected tag                                      
            if ( $data == $divId) 
            {
               echo "<option value=". $data ."selected>";
               echo $data ;
               echo "</option>";
            }
            else 
            {
               echo "<option value=". $data .">";
               echo $data;
               echo "</option>";
            }
          }

我可以在 php while 语句中添加 if/else 语句吗?这可能是我在下拉列表中没有得到任何数据结果的原因吗?

【问题讨论】:

  • 我看不出你的逻辑有什么问题。我也看不到你在哪里关闭选择标签。就这么简单。
  • @Len_D 感谢您对结束标记语法的捕获,但我在现有代码中有它,我只是忘记在帖子中包含它。

标签: php dynamic


【解决方案1】:

我想通了:

我需要像这样使用内联 if 语句:

<!-- Div Id -->                                        
<label for="divId" class="control-label">Div Id</label>
   <select class="form-control" name="divId" id="divId">
       <option value=" "></option>
       <?php
          while ($rowDivs = $divResult->fetch_assoc()) {

              $data = '37' .$rowDivs['div_id'];
              echo "<option value=$data" .($data == $divId ? ' selected="selected"' : '') . ">$data</option>";  
          }     
       ?>
   </select>

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2020-07-06
    • 2013-10-23
    相关资源
    最近更新 更多