PHP EDIT form. Previous dropdown data not populating

Viewed 53

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>
...
1 Answers

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.

Related