简体   繁体   中英

Setting a select option default to a previously set option

In my modal window, I have a select where a user can select a breakfast meal from my database. By selecting a meal and submitting it, a record is saved in the database. However, I would like to achieve that when the user enters into the same modal window, whatever meal they have selected before hand will appear as the default option. I have created a query that checks to see if a meal has already been selected (optionquery1). Any ideas to achieve this? Thanks

On a side note, I'm not sure that I'm setting the date php variable currently. The day, month and year is stored in the html hidden inputs but not sure how to extract the values.

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

Of course you can, you need to use the selected attribute on the <option> you want to show. First remove the selected attribute from the first <option> :

<option value="" disabled>Select your breakast</option>

Then in the foreach loop you need an if condition:

$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;} ?>

Notice I have removed the while loop since we are sure Mysql will return only one record ( WHERE ID = <ID> ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM