【问题标题】:Display previously selected value in drop down when editing record in mysqli在 mysqli 中编辑记录时在下拉列表中显示先前选择的值
【发布时间】:2017-12-02 13:39:54
【问题描述】:

我继承了一个小的 php 页面,它允许用户输入重定位请求,然后返回并编辑它们。

添加页面有许多下拉框,但编辑页面只是将它们显示为文本字段。如果您需要在编辑页面中选择不同的值,您需要知道下拉框中的所有值。

我正在尝试想办法在编辑页面的下拉框中显示之前从下拉框中选择的值,以便在需要时轻松地允许某人更改值。

我使用“选择”方法看到了一些其他类似的问题和答案,但我无法将其与我正在查看的代码联系起来。我不是 php 方面的专家,我在这里使用示例和解决方案来使事情顺利进行。在这种情况下,我无法将它们放在一起。

感谢任何帮助。

谢谢

<?php
$resultNames = $conn->query("SELECT txtCraftGroup FROM tblCraftGroup Order 
by txtCraftGroup");
if($resultNames) {
?>
<tr>
<td>Craft: </td>
<td><select name="craft">
<option value="0">Please Select</option>
<?php
while ($MCraft = $resultNames->fetch_assoc()) {
  echo "<option value=\"{$MCraft['txtCraftGroup']}\">";
  echo $MCraft['txtCraftGroup'];
  echo "</option>";
}

}else{
?>
<td>Craft: </td>
<td><input type="text" name="craft" value="<?php echo $MCraft; ?>"/><br/>
</td>
<?php
  }
  ?>
  </select><td>
  </tr>

【问题讨论】:

  • @Vanitha Kesavan 当下拉列表被填充并且其余字段也停止显示时,编辑屏幕上没有填充数据。也不确定 $craftval 来自哪里。我知道我似乎在用这个在黑暗中钓鱼。只是无法使解决方案发挥作用。

标签: php html mysqli drop-down-menu


【解决方案1】:

工作代码:

您可以使用选定的选项。 假设您有两种形式: 添加表格。 更新表格。 先将url中的值通过GET方式post出来,这样就可以在另一个页面访问字段值了。

<select name = "field_name">
<option value="<?php echo $_GET["field_name"]; ?>" selected > <?php echo 
$_GET["field_name"]; ?> </option>
{
 //you can add your dropdown condition here if you are fetching an array
}
</select>

【讨论】:

    【解决方案2】:
    To display the selected option in dropdown
    
    
    <td><select name="craft">
    <?php
    while ($MCraft = $resultNames->fetch_assoc()) 
    {  ?>
             <option value="<?php echo $MCraft['txtCraftGroup'] ;?>" 
    
              <?php if ($MCraft['txtCraftGroup']==$craftval) echo 'selected = "selected"'; ?> >
    
             <?php echo $MCraft['txtCraftGroup']; ?></option>
      <?php
        } 
    
    ?>        
    
    </select> </td>
    

    举例

    <td><select name="gender">
    <?php
    while ($row = $resultNames->fetch_assoc()) 
    {    ?>
    
        <option value="male" 
        <?php if ($row['gender']=="male")  echo 'selected = "selected" '; ?> >
          Male
        </option>
        <option value="female" 
                     <?php if ($row['gender']=="female")  echo 'selected = "selected" '; ?> >
        Female
        </option>
        <?php
    
     } 
    
    ?>        
            </select> </td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 2011-09-16
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      相关资源
      最近更新 更多