0

I have a simple order form with name (text), date (date), and then 2 sets of dropdowns. Each set contains a dropdown for flange and one for quantity. Basically the set repeats a couple times giving the user the ability to select a flange and then quantity with each set.

The forms sends all of the data (name, date, dropdown values, quantitys etc) to the database. I have created an edit page. When I go to edit an order all the data populates (including the quantity dropdowns) except for the flange dropdowns. They are just default blank.Is there a way to have the flange dropdown populate to the previously selected values?

<div style="display:block;">
          <label for="itemID">1:</label>
          <select id="itemID" name="itemID" required value="<?php echo $row['itemID']; ?>">
                    <option value="1">Flange 1 @ $100</option>
                    <option value="2">Flange 2 @ $100</option>
                    <option value="3">Flange 3 @ $50</option>
                    <option value="4">Flange 4 @ $50</option>
                    <option value="5">Flange 5 @ $50</option>
        </select>
  <label for="quantity"></label>
  <input type="number" id="quantity" name="quantity"  min ='1' max='50' style="width: 3em" required  value="<?php echo $row['quantity']; ?>">
  </div>
  <div style="display:block;">
          <label for="itemID2">1:</label>
          <select id="itemID2" name="itemID2" required value="<?php echo $row['itemID2']; ?>">
                    <option value="1">Flange 1 @ $100</option>
                    <option value="2">Flange 2 @ $100</option>
                    <option value="3">Flange 3 @ $50</option>
                    <option value="4">Flange 4 @ $50</option>
                    <option value="5">Flange 5 @ $50</option>
         </select>
  <label for="quantity2"></label>
  <input type="number" id="quantity2" name="quantity2"  min ='1' max='50' style="width: 3em" required  value="<?php echo $row['quantity2']; ?>">
  </div>
...
  • Does this answer your question? [How to set selected value of HTML select box with PHP](https://stackoverflow.com/questions/3747212/how-to-set-selected-value-of-html-select-box-with-php) – Don't Panic Sep 18 '22 at 09:39
  • Welcome to SO. Please try searching before posting - there are many answers to your question here already: https://stackoverflow.com/questions/32837873/set-selected-option-value-using-php, https://stackoverflow.com/questions/1336353/how-do-i-set-the-selected-item-in-a-drop-down-box, https://stackoverflow.com/questions/34737676/how-to-set-selected-value-in-html-dropdown-using-php, https://stackoverflow.com/questions/32837873/set-selected-option-value-using-php, https://stackoverflow.com/questions/38651772/set-selected-value-based-on-database-fetched-value ... – Don't Panic Sep 18 '22 at 09:42

1 Answers1

0

You have to set the selected attribute in the option that is currently selected. Like this

<?php
$value1 = $row['itemID'];
?>
  <label for="itemID">1:</label>
  <select id="itemID" name="itemID" required>
    <option value="1" <?php if ($value1=="1") {echo "selected";}?>>Flange 1 @ $100</option>
    <option value="2" <?php if ($value1=="2") {echo "selected";}?>>Flange 2 @ $100</option>
    <option value="3" <?php if ($value1=="3") {echo "selected";}?>>Flange 3 @ $50</option>
    <option value="4" <?php if ($value1=="4") {echo "selected";}?>>Flange 4 @ $50</option>
    <option value="5" <?php if ($value1=="5") {echo "selected";}?>>Flange 5 @ $50</option>
  </select>

And repeat for the 2nd lot.

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41