首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将select选项缺省设置为先前设置的选项

问将select选项缺省设置为先前设置的选项
EN

Stack Overflow用户
提问于 2018-04-09 00:54:09
回答 1查看 57关注 0票数 0

在我的模式窗口中,我有一个select,用户可以从我的数据库中选择一顿早餐。通过选择一顿饭并提交它,在数据库中保存一条记录。然而,我想要实现的是,当用户进入相同的模式窗口时,他们之前选择的任何饭菜都将显示为默认选项。我已经创建了一个查询,用于检查是否已经选择了一顿饭(optionquery1)。有什么想法可以实现这一点吗?谢谢

顺便说一句,我不确定当前是否设置了date php变量。日、月和年存储在html的隐藏输入中,但不确定如何提取这些值。

代码语言:javascript
复制
<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<div class="modal-body">
  <form action="my_planner.php" method="post">
  <!--<span class="test5"></span>-->
  <!--<span class="test"><input type="text" name="id_disabled" value="" disabled/>-->
  <input class="test" type="text" name="id1" value="" style="text-align:center; border:0px; font-weight:bold; font-size: 25px;"/>
  <hr class="my-4">
  <input type="hidden" name="id" value=""/>
  <input type="hidden" name="month" value=""/>
  <input type="hidden" name="year" value=""/>

<br>
<p id="breakfastTag"><br>Breakfast:</p>
<select id="breakfast" name="breakfast">
<option value="" disabled selected>Select your breakast</option>
<?
$meal='breakfast';
$date='<input type="hidden" name="id" value=""/>'.'<input type="hidden" name="month" value=""/>'.'<input type="hidden" name="year" value=""/>';
$query = $db->prepare("select * from recipe_category where categoryID=1");
$query->execute();

while ($results = $query->fetch()) {
    $recipeID=$results["recipeID"];
    $query3 = $db->prepare("select * from recipe where id=:recipeID");
    $dbParams3=array('recipeID'=>$recipeID);
    $query3->execute($dbParams3);
    $breakfast = $query3->fetchAll();

    $optionquery = $db->prepare("select * from user_planner where userID=:userID and date=:date and meal=:meal");
    $optionParams= array ('userID'=>$thisUser, 'date'=>$date,'meal'=>$meal);
    $optionquery->execute($optionParams);

    while ($results12 = $optionquery->fetch()) {
        $selectedmeal = $results12['recipeID'];
    }

    $optionquery1 = $db->prepare("select * from recipe where id=:recipeID");
    $optionParams1= array ('recipeID'=>$selectedmeal);
    $optionquery1->execute($optionParams1);

    while ($results123 = $optionquery1->fetch()) {
        $selectedrecipe = $results123['name'];
    }

    foreach($breakfast as $breakfast): ?>
        <option  value="<?= $breakfast['id']; ?>"><?= $breakfast['name']; ?></option>
    <?php endforeach;} ?>
</select>
EN

回答 1

Stack Overflow用户

发布于 2018-04-09 01:03:19

当然可以,您需要在要显示的<option>上使用selected属性。首先从第一个<option>中删除selected属性

代码语言:javascript
复制
<option value="" disabled>Select your breakast</option>

然后在foreach循环中,您需要一个if条件:

代码语言:javascript
复制
$results123 = $optionquery1->fetch();
$selectedrecipe = $results123['id'];

foreach($breakfast as $breakfast): ?>
    <option  value="<?= $breakfast['id']; ?>" <?php if ($breakfast['id'] == $selectedrecipe) echo 'selected' ?>><?= $breakfast['name']; ?></option>
<?php endforeach;} ?>

注意,我删除了while循环,因为我们确信Mysql将只返回一条记录(WHERE ID = <ID>)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49720336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档